473,804 Members | 2,265 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 3943
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
17883
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
2182
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
6737
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 I have to if it is. I am a bit confused about the arrays particularly. In normal situations,...
14
3110
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
2599
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
9342
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
2299
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
5187
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
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.