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
Solved

Pointer to array of structs (C)

Posted on 2011-03-25
6
642 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

809 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