• Status: Solved
• Priority: Medium
• Security: Public
• Views: 205

# function pointer

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
Vasconcelos
• 4
• 3
1 Solution

Commented:
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

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

Commented:
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

Commented:
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

Commented:
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 Commented:
thanks all :)
0

Commented:
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

Commented:
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

• 4
• 3
Tackle projects and never again get stuck behind a technical roadblock.