Jump to content

Using pointers to pointers to arrays as parameters

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I have the following code:
unsigned char numbers[10];

unsigned char *solution[10];


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

	for (unsigned char digit = 0; digit < 10; ++digit) {

		numbers[digit] = digit;

	}

	for (unsigned char col = 0; col < 9; ++col) {

		*solution[col] = numbers[col + 1];

	}

	return(0);

}

So I have an array containing possible values, and I have an array of 10 pointers, each of which will be pointing to one of the values. This allows me to easily change a certain value found more than once in the array, by just changing that value. For example, I could change numbers[1] to contain 5, and all pointers pointing to that array index would now be pointing to the value 5.

But now I want to have a pointer pointing to my array of pointers, so that I can use the increment operator ++ to assign the numbers to my array of pointers, and so that I can use that pointer as a parameter for another function. Something like this:
unsigned char numbers[20];

unsigned char *solution[10];

unsigned char ?solutionPtr;


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

	for (unsigned char digit = 0; digit < 20; ++digit) {

		numbers[digit] = digit;

	}

	?solutionPtr = ?solution;

	for (unsigned char col = 0; col < 9; ++col) {

		?solutionPtr++ = numbers[col + 1];

	}

	test(?solutionPtr);

	return(0);

}


void test(? solutionPtr) {

	for (unsigned char col = 10; col < 19; ++col) {

		?solutionPtr++ = numbers[col + 1];

	}

}

The question marks shows where I'm having doubt about what to write to make this work. Specifically I'm not sure how many asterisks I will need to use to make it work as intended.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I don't quite understand what you're saying.
#include <stdio.h>

void foo(int *ptr[10])
{
   int i;
   for ( i = 0; i < 10; ++i )
   {
      printf("ptr[%d] = %p -> %d\n", (int)i, (void*)ptr[i], *ptr[i]);
   }
}

int main(void)
{
   int numbers[10];
   int *solution[10];
   int i;
   for ( i = 0; i < 10; ++i )
   {
      numbers[i] = i;
      printf("array[%d] = %d\n", (int)i, numbers[i]);
   }
   for ( i = 0; i < 10; ++i )
   {
      solution[i] = &numbers[i];
      printf("solution[%d] = %p -> %d\n", (int)i, (void*)solution[i], *solution[i]);
   }

   foo(solution);
   return 0;
}

/* my output
array[0] = 0
array[1] = 1
array[2] = 2
array[3] = 3
array[4] = 4
array[5] = 5
array[6] = 6
array[7] = 7
array[8] = 8
array[9] = 9
solution[0] = 0022FF40 -> 0
solution[1] = 0022FF44 -> 1
solution[2] = 0022FF48 -> 2
solution[3] = 0022FF4C -> 3
solution[4] = 0022FF50 -> 4
solution[5] = 0022FF54 -> 5
solution[6] = 0022FF58 -> 6
solution[7] = 0022FF5C -> 7
solution[8] = 0022FF60 -> 8
solution[9] = 0022FF64 -> 9
ptr[0] = 0022FF40 -> 0
ptr[1] = 0022FF44 -> 1
ptr[2] = 0022FF48 -> 2
ptr[3] = 0022FF4C -> 3
ptr[4] = 0022FF50 -> 4
ptr[5] = 0022FF54 -> 5
ptr[6] = 0022FF58 -> 6
ptr[7] = 0022FF5C -> 7
ptr[8] = 0022FF60 -> 8
ptr[9] = 0022FF64 -> 9
*/
With what you have, you can't "increment" because you'd be pointing to unowned memory.

#3
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
If you mean because of this line:

unsigned char *solution[10];

then that is of course a mistake from my side. It should read:

unsigned char *solution[20];


#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Hmm. That would make it a bit harder to create a pointer to it if the type is different.
   int numbers[10];
   int *solution[10];
   int *(*foo)[10];
   foo = &solution;
Here, foo is a pointer to an array of 10 pointers to int. Is that kinda what you mean?

#5
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Yep, that's what I mean. I'm just confused as how to use it in my example, and passing it as a parameter, so that I can just iterate the solutionPtr, and assign the values that way.

In my example the types are the same though, all unsigned chars. But can you show me how I would then use that pointer in my example above?

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

ThemePark said:

Yep, that's what I mean. I'm just confused as how to use it in my example, and passing it as a parameter, so that I can just iterate the solutionPtr, and assign the values that way.

In my example the types are the same though, all unsigned chars. But can you show me how I would then use that pointer in my example above?
If you're confused about using it, then what problem are you trying to solve?

One more spin:
#include <stdio.h>

int main(void)
{
   unsigned char array[10];
   unsigned char *parray[5][10];
   unsigned char *(*pparray)[10] = &parray[0];
   int i, j;
   for ( i = 0; i < 10; ++i )
   {
      array[i] = i;
      for ( j = 0; j < 5; ++j )
      {
         parray[j][i] = &array[i];
      }
   }
   printf("pparray = %p\n", (void*)pparray);
   for ( i = 0; i < 10; ++i )
   {
      printf("(*pparray)[%d] = %d\n", (int)i, (int)*(*pparray)[i]);
   }
   ++pparray;
   printf("pparray = %p\n", (void*)pparray);
   for ( i = 0; i < 10; ++i )
   {
      printf("(*pparray)[%d] = %d\n", (int)i, (int)*(*pparray)[i]);
   }
   return 0;
}

/* my output
pparray = 0022FE90
(*pparray)[0] = 0
(*pparray)[1] = 1
(*pparray)[2] = 2
(*pparray)[3] = 3
(*pparray)[4] = 4
(*pparray)[5] = 5
(*pparray)[6] = 6
(*pparray)[7] = 7
(*pparray)[8] = 8
(*pparray)[9] = 9
pparray = 0022FEB8
(*pparray)[0] = 0
(*pparray)[1] = 1
(*pparray)[2] = 2
(*pparray)[3] = 3
(*pparray)[4] = 4
(*pparray)[5] = 5
(*pparray)[6] = 6
(*pparray)[7] = 7
(*pparray)[8] = 8
(*pparray)[9] = 9
*/


#7
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I am trying to solve the exact problem I described in my first post. I am confused and unsure of how many asterisks I need to use in the places where I placed a question mark in my original test code to make it work as I described. And I need it to be exactly like I wrote in my code, that is two functions which both can access my solutionPtr by transferring it as a parameter, a solutionPtr pointing to an array of 20 pointers, and each of those 20 pointers pointing to a value from my array of values. The values do not matter, it's being able to assign them using one pointer which I increment, that is important.

If you take my code from the first post and expand what you have written in your fourth post, that would become what I'm looking for. I just can't see what to write instead of the question marks, and it's only a test example I came up with to illustrate my problems, so it's not like you'll be doing all of my coding for me.

Update:
With the help of post #4 I was actually able to get everything to work and understand how it works. What confused me was that since I wanted a pointer to an array of pointers to an array, I didn't know whether to use 2, 3 or 4 asterisks to declare my solutionPtr, or if I had to use the array declaration [10] as well with any combination of the asterisks. But I see now that all I need is to declare my pointer as a pointer to another pointer, so 2 asterisks is the right answer. And the same goes for using it as a parameter, 2 asterisks as well.

I had also had some exceptions when trying to assign the number to whatever my pointer was pointing at, but I realized that even though I set my solutionPtr to point at the array of pointers, each pointer in that array is not actually pointing at anything. That is why I can't use:
**solutionPtr++ = numbers[col + 1];

because the second pointer (the one in the array) isn't pointing to any memory address, i.e. it hasn't had any memory allocated. So I found out that I need to do this:

*solutionPtr++ = &numbers[col + 1];

and it will work beautifully.

Edited by ThemePark, 05 February 2010 - 08:21 AM.