Allocating space for array of pointers?

P

Paminu

If I have this struct:

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test {
int x;
int y;
} container;


I would now like to make an array containing 4 pointers to this struct. But
I would also like to allocate space for this array:

int main(void)
{
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!

return 0;
}

Do I have to allocate space for each pointer at a time?
 
A

Artie Gold

Paminu said:
If I have this struct:

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test {
int x;
int y;
} container;


I would now like to make an array containing 4 pointers to this struct. But
I would also like to allocate space for this array:

int main(void)
{
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!

return 0;
}

And what might that error be? (Looks OK to me...)
Do I have to allocate space for each pointer at a time?

No.

HTH,
--ag
 
P

Paminu

Artie said:
Paminu said:
If I have this struct:

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test {
int x;
int y;
} container;


I would now like to make an array containing 4 pointers to this struct.
But I would also like to allocate space for this array:

int main(void)
{
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!

return 0;
}

And what might that error be? (Looks OK to me...)



test4.c:13: error: incompatible types in assignment

where line 13 contains:
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!



I have instead tried:

container *new[4];
int j;
for (j = 0; j < 4; j++)
{
new[j]=malloc(sizeof(container));
new[j]=NULL;

}

Then I get no error!
 
M

Martin Ambuhl

Paminu said:
If I have this struct:

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test {
int x;
int y;
} container;


I would now like to make an array containing 4 pointers to this struct. But
I would also like to allocate space for this array:

int main(void)
{
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!

As it should. You have already declared new as an array[4] of pointers
to container.

{
size_t i;
for (i = 0; i < 4; i++)
if (!(new = malloc(sizeof *new)))
{ /* handle error */ }
}

return 0;
}

Do I have to allocate space for each pointer at a time?

No, you need to allocate space for what each pointer points to.
 
K

Kenneth Brody

Artie Gold wrote:
[...]
container *new[4];
new=malloc(sizeof(container *) * 4); // THIS LINE GIVES AN ERROR!
[...]

And what might that error be? (Looks OK to me...)

"new" is an array of 4 pointers to "container", not a pointer to an
array of 4 "container"s.

It depends. If you want "new" to be an array of 4 pointers, then yes:

container *new[4]; /* As in the original code. */

/* Allocate each pointer. This can also be in a for-loop */
new[0] = malloc(sizeof(container));
new[1] = malloc(sizeof(container));
new[2] = malloc(sizeof(container));
new[3] = malloc(sizeof(container));

If you want it to point to an array of 4 containers:

container *new;
new = malloc(sizeof(container)*4);


BTW, this is a perfect example of how C++ is not a "superset" of C.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top