Pointer to array of structs (C)

Posted on 2011-03-25
Last Modified: 2012-05-11

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?
Question by:php-newbie
LVL 53

Expert Comment

ID: 35215959
I'm not sure what you mean, but you can access the struct members using :

LVL 32

Expert Comment

ID: 35215984
>> 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 ->
LVL 16

Expert Comment

ID: 35216949
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 )

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 13

Accepted Solution

Superdave earned 500 total points
ID: 35220176
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.

LVL 12

Expert Comment

ID: 35221983
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?"
LVL 30

Expert Comment

ID: 35280489
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now