473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Allocating space for array of pointers?

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?
Feb 9 '06 #1
3 3885
Paminu wrote:
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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 9 '06 #2
Nelu wrote:
Paminu wrote:
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
Feb 9 '06 #3
Nelu wrote:
Paminu wrote:
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.

Feb 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Philipp | last post by:
Hello, a very simple question: Ok I have a class MyClass with a constructor MyClass(int) (no constructor without argument defined) how can I make an array of pointers to objects of that class,...
3
by: Erik S. Bartul | last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate memory for it on the fly. i assume i declare, char **a; but how do i allocate memory for the pointers, so i can...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
6
by: Paminu | last post by:
If I have this struct: #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { int x; int y; } container;
3
by: UncleRic | last post by:
Greetings: I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs. This is what I found in theScripts' archive: ...
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.