Solved

Pointer to array of structs (C)

Posted on 2011-03-25
6
673 Views
Last Modified: 2012-05-11
Experts,

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?
0
Comment
Question by:php-newbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

Expert Comment

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

        my_struct_array_ptr->the_member
0
 
LVL 32

Expert Comment

by:phoffric
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 ->
0
 
LVL 16

Expert Comment

by:HooKooDooKu
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 )

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Accepted Solution

by:
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.

0
 
LVL 12

Expert Comment

by:trinitrotoluene
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?"
my_struct_array_ptr[0].yourstructuremember;
my_struct_array_ptr[1].yourstructuremember;
0
 
LVL 30

Expert Comment

by:Axter
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:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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