Solved

Pointer to array of structs (C)

Posted on 2011-03-25
6
655 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
Technology Partners: 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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 125
Issues with C++ Class 19 120
Indy 10 not Receiving UDP broadcast 3 56
C++ Code Issue 4 48
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

739 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