Solved

# function pointer

Posted on 2009-12-28
199 Views
I want pass to the function an array with a size  them transform it  into a new array with a bigger size and them return the new array from a function, im putting zeros between the numbers i also dont know if im doing it right.

#DEFINE SIZE = 100

int* transform_array(int *array)
{
int i = 0;
int num = 0;
int *temp = (int*)malloc(100*sizeof(int));
for(i=0; i < SIZE; i++)
{
num = (*array + i);
*(temp + i) = num;
*(temp + i + 1) = 0;
}
return temp;
}

int main()
{
int v[6] = {1, 1, 2, 1, 1, 2};
int i = 0;
aux = transform_array(v, 6);
for(i=0; i < SIZE; i++)
{
printf("%d\n",*(aux + i));
}
}
0
Question by:Vasconcelos

LVL 53

Expert Comment

Do you mean you want the function to create a new dynamically allocated array, and copy the contents of the original array in it ? (while initializing the other values to 0)

If so, something like this should do the trick :
``````#DEFINE SIZE 100

int *transform_array(int *array, size_t size) {

int i = 0;

int *temp = (int*) malloc(SIZE * sizeof(int));

for (i = 0; i < size; ++i) {

temp[i] = array[i];

}

for (; i < SIZE; ++i) {

temp[i] = 0;

}

return temp;

}

int main(void) {

int v[6] = { 1, 1, 2, 1, 1, 2 };

int i = 0;

int *aux = transform_array(v, 6);

for (i = 0; i < SIZE; ++i) {

printf("%d\n", aux[i]);

}

return 0;

}
``````
0

LVL 53

Expert Comment

Note that you should probably also free the allocated memory again once you don't need it any more.
0

LVL 31

Expert Comment

int* transform_array(int *array)
aux = transform_array(v, 6);

You define transform_array with one parameter, but use it with two. Make definition and usage consistent.
0

LVL 31

Expert Comment

This looks like a FFT problem.
Here is an approach to alternate the original array values with 0's, and fill the remainder of the bigger array with 0's.
``````#define SIZE 100

int* transform_array(int *array, int arraySize)

{

int i; // index of new malloc array

int k; // index of input array

int num = 0; // value of input array element

int *temp = (int*)malloc(100*sizeof(int));

for(i=0, k=0; i < SIZE; i++)

{

if( i%2 == 0 && k < arraySize )

{

num = *(array + k);

*(temp + i) = num;

k++;

}

else {

*(temp + i) = 0;

}

}

return temp;

}

int main()

{

int v[6] = {1, 1, 2, 1, 1, 2};

int i = 0;

int * aux;

aux = transform_array(v, 6);

for(i=0; i < SIZE; i++)

{

printf("%d\n",*(aux + i));

}

free(aux);

}
``````
0

LVL 31

Accepted Solution

Now, if this were part of a FFT algorithm, and running into a DSP processor, the following might be more efficient:
``````#define SIZE 100

int* transform_array(int *array, int arraySize)

{

int k; // index of input array

int *temp = (int*)calloc( SIZE, sizeof(int) );

int *bigArray = temp;

for(k=0; k < arraySize; k++)

{

*temp = *array++;

temp += 2;

}

return bigArray;

}

int main()

{

int v[7] = {1, 1, 2, 1, 1, 2};

int i = 0;

int * aux;

aux = transform_array(v, sizeof(v)/sizeof(v[0]) );

for(i=0; i < SIZE; i++)

{

printf("%d\n",*(aux + i));

}

free(aux);

}
``````
0

Author Closing Comment

thanks all :)
0

LVL 53

Expert Comment

Vasconcelos, you might get better and faster answers if you give feedback and clearly explain what it is you need. For example : if what I posted didn't help, it would be useful to explain why it doesn't help, so I can adapt to your requirements. Otherwise, you just leave us guessing, and the person who first guesses right (if ever) is lucky.
0

LVL 31

Expert Comment

fyi - my FFT and DSP references were a guess - just a curiosity statement. However, these points are unrelated to the question and both code solutions. The author requested functionality to insert 0's between original array values. That is the functionality I tried to provide.

The 2nd code post is probably more efficient in general (by general, I mean larger input array sizes); and probably very efficient in a DSP processor having parallelism, but where penalties are paid for branching. My comments about FFT and DSP was only to enrich the basic discussion so that others who may happen upon this solution may find additional benefit.
0

## Featured Post

### Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.