0

I have a requirement to use pointer to a array of structures as defined below:

typedef void (*fptr)(uint8 arg);
typedef struct sTest
{
  uint8 u8Mem1;
  uint8 u8Mem2;
  fptr fptr1;
}tTest;

Now I have define an array of this structre as follow:

tTest sTestStructure[20];

So now I want define a pointer and assign the array of stuct{sTestStructure[20]} to this pointer.Then access the member of this structure using the pointer define. So please let me know how can I define this pointer and also how to access the members using pointer.

5
  • you can use pointet *ptrsturct that is tTest *ptstruct = sTestStructure; Commented Dec 6, 2016 at 14:34
  • Your two choices are to use a pointer to first element (just write sTestStructure) or pointer to array &sTestStructure which has type tTest (*)[20]. The first choice is far more common in C. Commented Dec 6, 2016 at 14:37
  • tTest** ptr = &sTestStructure ? (litterally a pointer to an array of your structure) Commented Dec 6, 2016 at 14:39
  • @Fefux: wrong type - the expression &sTestStructure has type tTest (*)[20], not tTest **. Commented Dec 6, 2016 at 16:27
  • Yes, my comment is totally wrong. Commented Dec 6, 2016 at 16:44

2 Answers 2

1

Remember that unless it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" (T [N]) will be converted ("decay") to an expression of type "pointer to T" (T *), and the value of the expression will be the address of the first element of the array.

This naturally leads us to the following:

tTest *ptr = sTestStructure;  // note no & operator!

The expression sTestStructure is implicitly converted from type "20-element array of tTest" to "pointer to tTest", and the value of the expression is the same as &sTestStructure[0]. To access any element of sTestStructure, you could simply index into ptr as you would sTestStructure:

ptr[i].u8Mem1 = some_value;                // sTestStructure[i].u8Mem1 = some_value
printf( "%" PRIu8 "\n", ptr[j].u8Mem2 );

Remember that the subscript operation a[i] is defined as *(a + i); given the address a, offset i elements from that address and defereference the result. Thus, the [] operator implicitly dereferences ptr, which is why we use the . operator to access each struct member.

Alternately, you could also access struct members using the -> operator, and advance the pointer as necessary:

tTest *ptr = sTestStructure;
while( ptr->u8Mem1 != some_value )  // no subscript operation here
  ptr++;

The expression ptr->u8Mem1 is equivalent to (*ptr).u8Mem11, which is equivalent to ptr[0].u8Mem1.

So what happens if we decide to use &sTestStructure instead? Since sTestStructure is the operand of the unary & operator, the conversion rule above doesn't apply; instead of getting a pointer to a pointer to tTest, we get a pointer to a 20-element array of tTest, or:

tTest (*arrPtr)[20] = &sTestStructure;

This presents a bit more of a challenge, since we have to dereference arrPtr before we can index into it:

(*arrPtr)[i].u8Mem1 = some_value;
printf( "%" PRIu8 "\n", (*arrPtr)[j].u8Mem2 );

Since a[i] is defined as *(a + i), the expression *arrPtr can also be written as arrPtr[0] (*arrPtr == *(arrPtr + 0) == arrPtr[0]). So those lines could also be written as

arrPtr[0][i].u8Mem1 = some_value;
printf( "%" PRIu8 "\n", arrPtr[0][j].u8Mem2 );

As should be evident from those last couple of lines, you normally don't see this form of an array pointer unless you're dealing with multi-dimensional arrays. Remember our conversion rule, where an expression of type T [N] is converted to an expression of type T *? Well, replace T with an array type like Q[M], and we get the conversion from "N-element array of M-element array of Q" (Q [N][M]) to "pointer to M-element array of Q" (Q (*)[M]):

tTest sTestStructure[10][20];
tTest (*arrPtr)[20] = sTestStructure;


  1. Postfix . has higher precedence than unary *, so *ptr.u8Mem1 would be parsed as *(ptr.u8Mem1), which is not what you want here.

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

Comments

0

They already told you how to define the pointer, now if you want to access a property would be *pointer->property.....

Comments

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.