473,583 Members | 3,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(sizeo f(container));
container->content="big ";
container->tt=malloc(size of(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 3914
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(sizeo f(container));
container->content="big ";
container->tt=malloc(size of(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_el se;
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(sizeo f(container));
container->content="big ";
container->tt=malloc(size of(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_el se;
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(sizeo f(container));
container->content="big ";
container->tt=malloc(size of(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_el se;


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
17856
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, calling the constructor with the index number as argument? <code> int N = 22; pointerArray = new MyClass*;
3
2165
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 then allocate to the pointers to which those pointers point? essentially lets say i during runtime deturmine i must allocate space for 60
15
6705
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 function. Is there any general rules that tell me when to allocate memory? I thought I don't have to if it is a variable that's not a pointer, and...
14
3082
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 the static way. For instance, if I want to declare a matrix 20x10 of integers and then to access matrix element: int matrix; int a;
10
2563
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 = malloc(sizeof(*arr) * 4); /* I want to dynamically allocate
6
458
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
9305
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: This is what I'm toying with: #include <stdio.h>
5
2281
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 ptrLogArray = new structDisplayLogData *; // iCount = 50 structDisplayLogData is a strcuture of data Thanks
6
5165
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 = malloc(n*sizeof(r)) and (to a degree) by the surrounding error checking.
0
7893
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8188
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6574
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.