• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Pointer to array of structs (C)


I'm trying to figure out how to work with pointers to array of structs. Consider the code below.

MyStruct my_struct_array[10] = {0};
MyStruct *my_struct_array_ptr = &my_struct_array[0];

How do I check if the my_struct_array_ptr  elements are NULL?
How do I access the my_struct_array_ptr  elements, e.g. my_struct_array_ptr[0]->my_struct_name?
1 Solution
I'm not sure what you mean, but you can access the struct members using :

>> How do I check if the my_struct_array_ptr  elements are NULL?

Is this what you want:
if( my_struct_array_ptr == NULL ) {
   // the pointer is NULL
else {
   // the pointer is (hopefully) pointing to a MyStruct object

Open in new window

>> my_struct_array_ptr[0]->my_struct_name?
my_struct_array_ptr[0] is not a pointer. You have deferenced it, so it now represents a struct object. So use . instead of ->
Here's some information that might help to break it down for you:

First of all, an array and a pointer are the same thing, except that an array allocates memory and it's value is read-only.  

Consider the following two lines of code:
MyStruct MyStructArray[10];
MyStruct* pMyStruct = &(MyStrcutArray[0]);

At this point, the variables MyStructArray and pMyStruct are both pointers to the 1st MyStruct element.  You could use pMyStruct and MyStructArray interchangably.  For example, the following if statement will always be TRUE

if( MyStructArray[5].Element1 == pMyStruct[5].Element1 )

But like I said, the array is read-only and the pointer can change.  So if I update the pointer with the following...
pMyStruct = &( MyStructArray[2] );
... then pMyStruct and MyStructArray are NOT equal.  The now point at different data.  But because pMyStuct points to the 2nd element, the follow if statement is again ALWAYS going to be TRUE:

if( MyStructArray[5].Element1 == pMyStruct[3].Element1 )

However, generally, when you have a pointer so something, you don't treat is like an array.  So when you have a pointer to a structure, you simply use the '->' operator in place of the '.' operator.  As another example, here is another IF statement that will ALWAYS be TRUE

pMyStruct = &( MyStructArray[2] );
if( MyStructArray[2].Element1 == pMyStruct->Element1 )

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Probably your declarations don't do what you think they do.

> How do I check if the my_struct_array_ptr  elements are NULL?

my_struct_array_ptr is a pointer, not an array, so it doesn't have "elements".  The thing it points to is an array, but it doesn't have pointers so there's no equivalent of "NULL".

How do I access the my_struct_array_ptr  elements, e.g. my_struct_array_ptr[0]->my_struct_name?

Either my_struct_array_ptr[0].my_struct_name or
my_struct_array_ptr->my_struct_name, but not both at once.
Also (*my_struct_array_ptr).my_struct_name would work.

Maybe what you want is:

MyStruct *my_struct_ptr_array[10];

I.e. an array of pointers to structures.  But then you have to initialize each of the pointers to point to a structure.

trinitrotolueneDirector - Software EngineeringCommented:
just adding to phoffric's comment. It is really simple.

>>>>"How do I check if the my_struct_array_ptr  elements are NULL?"
MyStruct *my_struct_array_ptr = new MyStruct[10];

now you really don't have to check for NULL, the standard guarantees that if new fails it will raise a bad_alloc exception. my_struct_array_ptr points to the first MyStruct element in the array

>>>>"How do I access the my_struct_array_ptr  elements, e.g. my_struct_array_ptr[0]->my_struct_name?"
I think  what you want is a pointer of pointers
int MyQty = 12;
MyStruct **MyArray = NULL;
MyArray = (MyStruct **)malloc(MyQty*sizeof(MyStruct*));

Now you can set above array of pointers, and nullify the last pointer.

Check out the following code for 2D array to give you some ideas:

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now