Allocating space for array of pointers?

P

Paminu

If I have this struct:

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

Now I would like to make an array of 5 pointers to this struct:

int main(void){
container *cp[5];
cp=malloc(sizeof(container));
container->content="big";
container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES ERROR!

return 0;


}

But how do I allocate space for these pointers? Do I have to do it for each
one at a time?
 
N

Nelu

Paminu said:
If I have this struct:

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

Now I would like to make an array of 5 pointers to this struct:

int main(void){
container *cp[5];
cp=malloc(sizeof(container));
container->content="big";
container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES ERROR!

return 0;


}

But how do I allocate space for these pointers? Do I have to do it for each
one at a time?
You wrote container *cp[5]. That means you already allocated space
for 5 pointers to container objects. cp[0]..cp[4] are all pointers
pointing who knows where... Now, if you want to allocate space
for a container object and make cp[3] point to it, you could do:
cp[3]=malloc(sizeof(container));
and
cp[3]->x=something;
cp[3]->y=something_else;
use free(cp[3]) to free the allocated space that cp[3] points to.

If you have a global variable:
container gcp;
you can say cp[3]=&gcp;
You should not free the pointer as you haven't allocated space,
the program did. Also, pay attention if you do that with auto
(local) variables as they are destroyed when the function, they
are defined in, ends.
 
P

Paminu

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

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

Now I would like to make an array of 5 pointers to this struct:

int main(void){
container *cp[5];
cp=malloc(sizeof(container));
container->content="big";
container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES
ERROR!

return 0;


}

But how do I allocate space for these pointers? Do I have to do it for
each one at a time?
You wrote container *cp[5]. That means you already allocated space
for 5 pointers to container objects. cp[0]..cp[4] are all pointers
pointing who knows where... Now, if you want to allocate space
for a container object and make cp[3] point to it, you could do:
cp[3]=malloc(sizeof(container));
and
cp[3]->x=something;
cp[3]->y=something_else;
use free(cp[3]) to free the allocated space that cp[3] points to.

If you have a global variable:
container gcp;
you can say cp[3]=&gcp;
You should not free the pointer as you haven't allocated space,
the program did. Also, pay attention if you do that with auto
(local) variables as they are destroyed when the function, they
are defined in, ends.


sorry tried to cancel this message as soon as possible, because I was
writing another more specfic one
 
P

Paminu

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

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

Now I would like to make an array of 5 pointers to this struct:

int main(void){
container *cp[5];
cp=malloc(sizeof(container));
container->content="big";
container->tt=malloc(sizeof(container *)*5); //THIS LINE GIVES
ERROR!

return 0;


}

But how do I allocate space for these pointers? Do I have to do it for
each one at a time?
You wrote container *cp[5]. That means you already allocated space
for 5 pointers to container objects. cp[0]..cp[4] are all pointers
pointing who knows where... Now, if you want to allocate space
for a container object and make cp[3] point to it, you could do:
cp[3]=malloc(sizeof(container));
and
cp[3]->x=something;
cp[3]->y=something_else;

But what if I would like all the pointers: cp[0]...cp[0] to point to NULL?
Do I need a for loop or is it possible to it with one commando?

The same thing when I want to allocate space for the object that the
pointers point to, I can do it in a for loop but would like to know if I
could do it with malloc only one time.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top