# Creating a pointer to an array of structs

• 12-26-2003
Jonathan_ingram
Creating a pointer to an array of structs
Hello, I was wondering if someone could please point out the incorrect use of syntax below, I want to create a pointer to an array of structs and then malloc that amount of memory....I currently have:

Particle *pParticle[MAX_NUMBER_PARTICLES];

pParticle = (Particle*)(malloc(MAX_NUMBER_PARTICLES * sizeof(Particle)));

Should I use a type of loop to malloc each array element in turn instead?

Thanks again.
• 12-26-2003
erikj
Two options

First one

Particle *pParticle[MAX_NUMBER_PARTICLES];

for(i=0;i<MAX_NUMBER_PARTICLES;i++)
pParticle[i] = (Particle*)(malloc(sizeof(Particle)));

second one

Particle *pParticle;

pParticle = (Particle*)(malloc(MAX_NUMBER_PARTICLES * sizeof(Particle)));
• 12-26-2003
XSquared
But an array is a pointer... so it would just be:

Code:

```Particle **pParticle; ( *pParticle ) = malloc( sizeof( Particle * MAX_NUM_PARTICLES ) );```
• 12-27-2003
Dave_Sinkula
>But an array is a pointer

The penalty for repeating such heresy is to review this.
• 12-27-2003
Fahrenheit
Quote:

Originally posted by Dave_Sinkula
>But an array is a pointer

The penalty for repeating such heresy is to review this.

Code:

`char abc[5] ;`
abc POINTS to statically allocated memory, correct?
• 12-27-2003
Prelude
>( *pParticle ) = malloc( sizeof( Particle * MAX_NUM_PARTICLES ) );
This is incorrect. The multiplication should not be an expression for the sizeof operator. This is what you want:
Code:

`malloc( sizeof( Particle ) * MAX_NUM_PARTICLES );`
Let's move to the other side of the assignment operator for a moment:
Code:

`( *pParticle )`
What does pParticle point to? If you said "Nothing predictable, and certainly nothing this program owns" you get a cookie.

>But an array is a pointer...
...when used as an expression.
• 12-27-2003
XSquared
Sorry 'bout that. I missed the closing parentheses on the sizeof( ) statement.

So how would you do it? My way was just a guess.

Would you have to do pParticle = malloc( sizeof( Particle * ) ) first?
• 12-27-2003
Prelude
>Would you have to do pParticle = malloc( sizeof( Particle * ) ) first?
It depends on what you want. If you want a dynamic array of pointers to Particle (as seems likely) then you would do this:
Code:

`pParticle = malloc ( N * sizeof *pParticle );`
Where N is the number of items you want in the array. You can use sizeof ( Particle * ), but that tends to confuse people, so taking the size of *p where p is the pointer being assigned to is better. It also improves maintainablility.
• 12-31-2003
Salem
> abc POINTS to statically allocated memory, correct?
No it doesn't point to the memory, it is the memory allocated to the array, and abc is the name by which those 5 chars are known.

In some contexts, the name "abc" gets converted into "a pointer to the first element", but that doesn't affect the array itself - for example
strcpy( abc, "yes" );
But then you could also write
strcpy( &abc[0], "yes" );

int foo = 2;
foo is the memory location containing 2, not a pointer to a memory location containing 2.