0

Please assist with below question on pointers to arrays. I have 20 arrays that are each 350 elements long. I need to pass the address of a select 3 out of the 20 arrays, to an array of pointers. Then later in my code I need to access individual elements within the arrays, within the array of pointers. However I am unsure as to the syntax, please comment as to whether the below is correct.

unsigned short      Graph1[350];
unsigned short      Graph2[350];
unsigned short      Graph3[350];
...       ...          ...
unsigned short      Graph19 [350];
unsigned short      Graph20 [350];
unsigned short      *ptr_Array[3];
...
*ptr_Array[0] = &Graph6;    // Passing the address of array Graph6, into the array of pointers.
*ptr_Array[1] = &Graph11;   // Passing the address of array Graph11, into the array of pointers.
*ptr_Array[2] = &Graph14;   // Passing the address of array Graph14, into the array of pointers.
...
Varriable1 = *ptr_Array[1]+55   // Trying to pass the 55th element of Graph11 into Varriable1. 
4
  • 3
    Not an answer: 20 arrays that are each 350 elements why don't you use 2-d array? Commented Apr 21, 2016 at 4:52
  • The array itself should be a pointer, isn't it? Commented Apr 21, 2016 at 5:01
  • @Rolice No array is not a pointer. It depletes to a pointer if used in expression or passed as argument to function. Commented Apr 21, 2016 at 5:02
  • Ah, thanks @MohitJain. I saw dereferencing and appeared strage to me. Commented Apr 21, 2016 at 5:03

2 Answers 2

2

The expression *ptr_Array[1]+55 is wrong multiple times, because of operator precedence.

The compiler sees it as (*(ptr_Array[1]))+55, that is it takes the second pointer in ptr_Array and dereferences it to get the first value, and add 55 to that value, which is not what you want. You need to explicitly use parentheses like *(ptr_Array[1]+55). Or simply ptr_Array[1][55].


And you should really consider the comment by Mohit Jain. Instead of having 20 different variables, just use one:

unsigned short Graph[20][350];
Sign up to request clarification or add additional context in comments.

Comments

2

*ptr_Array[0] = &Graph6; is wrong. It should be:

ptr_Array[0] = Graph6; /* or &Graph6[0] */

Type of ptr_Array is array 3 of pointer to unsigned short. ptr_Array[0] has type of pointer to unsigned short and *ptr_Array has type unsigned short.

Type of Graph6 is array 350 of unsigned short which will deplete to pointer to unsigned short if used in an expression.


Varriable1 = *ptr_Array[1]+55 is also wrong. To pass the 55th element, use

Varriable1 = ptr_Array[1][55];

2 Comments

One reason &Graph6 is wrong is that the type of &Graph6 is unsigned short (*)[350] (pointer to an array of 350 unsigned short), whereas each element of ptr_Array is meant to be an unsigned short *, and the two types are visibly quite different — which is why the compiler is complaining about pointer type mismatches.
Varriable1 = *ptr_Array[1][55]; should be Varriable1 = ptr_Array[1][55];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.