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.

2
  • 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. Commented Feb 12, 2014 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. Commented Feb 12, 2014 at 11:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

He's not allowed to change the function signature apparently (see question for details) - probably homework?
Thanks Paul, I missed that one!
@ Paul: Your comment seems funny, Anyway thanks for the support
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;
}

Comments

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.