1

This is a sample code of my program.

I have a function named function which is used to return an integer value and an integer array. I have to pass them as pointers (The signature of function cannot be changed). So I wrote the below code. Here I have to assign the values to the double pointer passed to the function.

void function(unsigned int* count, unsigned int ** array)
{
  for(unsigned int i = 0; i<4;i++)
  {
    //array[i] = (unsigned int*)i*10;
  }
  *count = 4;
}

int main()
{

  unsigned int count;
  unsigned int array2[4];
  function(&count, (unsigned int**)&array2);

  return 0;
}

But the above code is not working.

pmr
  • 58,701
  • 10
  • 113
  • 156
Aneesh Narayanan
  • 3,220
  • 11
  • 31
  • 48
  • 3
    your program doesn't produce any observable output. how could it demonstrate your problem? at some point, you have to **test** it to see if it works. – Karoly Horvath Feb 12 '14 at 10:38
  • My primary need was to fill the array 'array2' in the function 'function' with the specified values. I was debugging the code in VS to check whether it was working properly or not. – Aneesh Narayanan Feb 12 '14 at 11:53

3 Answers3

2

By reusing concepts I suppose you already know, you might use a pointer to the first element of the array and then pass the pointer's address to the function.

In that way you would have a double pointer to an unsigned int which is the first element of the array:

void function(unsigned int* count, unsigned int ** array)
{
  for(unsigned int i = 0; i<4;i++)
  {
    (*array)[i] = i*10; // WATCH OUT for the precedence! [] has higher precedence!
  }
  *count = 4;
}

int main(int argc, char* argv[])
{

  unsigned int count;
  unsigned int array2[4];
  unsigned int *pointer_to_array = &array2[0];
  function(&count, (unsigned int**)&pointer_to_array);

  return 0;
 }

As a sidenote:

If you could change the signature this would have made more sense:

void function(unsigned int* count, unsigned int * array)
{
  for(unsigned int i = 0; i<4;i++)
  {
    array[i] = i*10;
  }
  *count = 4;
}

int main(int argc, char* argv[])
{

  unsigned int count;
  unsigned int array2[4];
  function(&count, array2);

  return 0;
 }

The reason the above works is because when you pass an array into a function (directly or with an explicit pointer to that array) it essentially becomes a pointer. This is called array decaying.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
1
void function(unsigned int* count, unsigned int ** array){
    for(unsigned int i = 0; i<4;i++){
        (*array)[i] = i*10;
    }
    *count = 4;
}

int main(){
    unsigned int count;
    unsigned int array2[4];
    unsigned int *p = &array2[0];
    function(&count, &p);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

To assign to an array that is passed by pointer, first dereference it, then assign the value.

void function(unsigned int* count, unsigned int ** array)
{
  for(unsigned int i = 0; i<4;i++)
  {
    (*array)[i] = i*10;
  }
  *count = 4;
}
Thomas Ruiz
  • 3,611
  • 2
  • 20
  • 33