473,503 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic memory allocation of an array of pointers to structures

19 New Member
Hi,

I've been trying to allocate dynamicaly an array of pointers to structures. As it didn't work, I tried to reduce the problem as much as possible. This is what I have :

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef  struct
  6. {
  7.   int x;
  8.   int y;
  9. }struct_p;
  10.  
  11.  
  12. int main()
  13. {
  14.   int i,n=2;
  15.   struct_p *p;
  16.   p=calloc(n,sizeof(struct_p*));
  17.   for (i=0;i<n;i++)
  18.     {
  19.       p[i]->x=0;
  20.       p[i]->y=1;
  21.     }
  22.  }
  23.  
  24.  
When compiling it with gcc, I get the errors:


new.c:18: error: invalid type argument of ‘->’
new.c:19: error: invalid type argument of ‘->’


I thought that by using calloc the way I did it, I had defined an array of pointers to structures. So the use of 'p[i]->x' for example, should give the content of the strucure field 'x' in the structure of adress 'p[i]'. Where am I wrong? What should I do?

Regards
Aug 17 '07 #1
10 7484
sanctus
84 New Member
Hi,

I've been trying to allocate dynamicaly an array of pointers to structures. As it didn't work, I tried to reduce the problem as much as possible. This is what I have :

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef  struct
  6. {
  7.   int x;
  8.   int y;
  9. }struct_p;
  10.  
  11.  
  12. int main()
  13. {
  14.   int i,n=2;
  15.   struct_p *p;
  16.   p=calloc(n,sizeof(struct_p*));
  17.   for (i=0;i<n;i++)
  18.     {
  19.       p[i]->x=0;
  20.       p[i]->y=1;
  21.     }
  22.  }
  23.  
  24.  
When compiling it with gcc, I get the errors:


new.c:18: error: invalid type argument of ‘->’
new.c:19: error: invalid type argument of ‘->’


I thought that by using calloc the way I did it, I had defined an array of pointers to structures. So the use of 'p[i]->x' for example, should give the content of the strucure field 'x' in the structure of adress 'p[i]'. Where am I wrong? What should I do?

Regards
p[i] is no more an adress...p[i] is an element of an array and not a pointer to an element so you can't use ->. How you use calloc you allocate memory for an array of 2 objects of size of struct_p, so that should be fine. I think it should work if you replace '->' by '.'
Regards
Aug 17 '07 #2
bimbam
19 New Member
p[i] is no more an adress...p[i] is an element of an array and not a pointer to an element so you can't use ->. How you use calloc you allocate memory for an array of 2 objects of size of struct_p, so that should be fine. I think it should work if you replace '->' by '.'
Regards
Thank you for your answer.

I used sizeof(struct_p *) and not sizeof(struct_p). Does'nt it make any difference? I thought that, this way, the elements of the array would be pointers.

Regards
Aug 17 '07 #3
sanctus
84 New Member
Thank you for your answer.

I used sizeof(struct_p *) and not sizeof(struct_p). Does'nt it make any difference? I thought that, this way, the elements of the array would be pointers.

Regards
Actually I don't know well the operator sizeof, but from what I understand sizeof doesn't define any type (i.e. pointer or variable) just giving the size in octets. Considering this you should use sizeof(struct_p), because you want allocate memory to p so that it can hold two objects of type struct_p.
Using sizeof(struct_p *) it means that p can hold 2 objects of size of a pointer to struct_p.
I'm not sure about this, but I think it is this way.

Did you still get errors using the '.' instead of the '->' ?
Aug 17 '07 #4
bimbam
19 New Member
Actually I don't know well the operator sizeof, but from what I understand sizeof doesn't define any type (i.e. pointer or variable) just giving the size in octets. Considering this you should use sizeof(struct_p), because you want allocate memory to p so that it can hold two objects of type struct_p.
Using sizeof(struct_p *) it means that p can hold 2 objects of size of a pointer to struct_p.
I'm not sure about this, but I think it is this way.

Did you still get errors using the '.' instead of the '->' ?

It works with 'sizeof(struct_p)' (and '.'). You were right. Thank you for your help.

Regards
Aug 17 '07 #5
bimbam
19 New Member
Actually I don't know well the operator sizeof, but from what I understand sizeof doesn't define any type (i.e. pointer or variable) just giving the size in octets. Considering this you should use sizeof(struct_p), because you want allocate memory to p so that it can hold two objects of type struct_p.
Using sizeof(struct_p *) it means that p can hold 2 objects of size of a pointer to struct_p.
I'm not sure about this, but I think it is this way.

Did you still get errors using the '.' instead of the '->' ?

But in that case, how would you allocate dynamically the memory of an array of pointers if not with 'p=calloc(n,sizeof(struct_p*))'?

Regards
Aug 17 '07 #6
JosAH
11,448 Recognized Expert MVP
But in that case, how would you allocate dynamically the memory of an array of pointers if not with 'p=calloc(n,sizeof(struct_p*))'?

Regards
If you want to allocate n of those pointers, the way you described it is the way
to do it; but when you want to assign whatever calloc returned you need a
struct_p** (note the double star) because it points to zero or more of those
struct_p*s (not the single star).

If you do it like this you have an array of struct pointers but none of the pointers
point to such a struct yet, i.e. you have to initialize the array elements (the pointers)
if you want to dereference anything from them.

kind regards,

Jos
Aug 17 '07 #7
bimbam
19 New Member
If you want to allocate n of those pointers, the way you described it is the way
to do it; but when you want to assign whatever calloc returned you need a
struct_p** (note the double star) because it points to zero or more of those
struct_p*s (not the single star).

If you do it like this you have an array of struct pointers but none of the pointers
point to such a struct yet, i.e. you have to initialize the array elements (the pointers)
if you want to dereference anything from them.

kind regards,

Jos
Thanks a lot for your answer.

Everything seems to work. I just want to be sure that I did the memory allocation properly so that nothing will be overwriten later and no memory was wasted.

Could you quickly have a look at these few lines to see if everything is "perfect"? Do you have suggestions to improve it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef  struct
  6. {
  7.   int x;
  8.   int *y;
  9. }struct_p;
  10.  
  11.  
  12. int main()
  13. {
  14.   int i,j,n=3;
  15.   int x,y;
  16.   struct_p **p;  
  17.   p=malloc(n*sizeof(struct_p*));
  18.  
  19.   for (i=0;i<n;i++)
  20.     {
  21.       p[i]=malloc(sizeof(struct_p)); 
  22.       p[i]->x=0;
  23.       p[i]->y=malloc(n*sizeof(int));
  24.     }
  25.  
  26.   for (i=0;i<n;i++)
  27.     {
  28.       printf("p[%d]->x=%d | ",i,p[i]->x);
  29.       for (j=0;j<n;j++)
  30.     printf("p[%d]->y[%d]=%d ",i,j,p[i]->y[j]);
  31.       printf("\n");
  32.     }
  33.  }
  34.  
  35.  
The output is as expected:

p[0]->x=0 | p[0]->y[0]=0 p[0]->y[1]=0 p[0]->y[2]=0
p[1]->x=0 | p[1]->y[0]=0 p[1]->y[1]=0 p[1]->y[2]=0
p[2]->x=0 | p[2]->y[0]=0 p[2]->y[1]=0 p[2]->y[2]=0


Regards
Aug 17 '07 #8
JosAH
11,448 Recognized Expert MVP
Yep, that's all correct as far as I can see now.

kind regards,

Jos

edit: you could've done 'calloc(n, sizeof(struct_p*))' instead of malloc(n*sizeof(struct_p*))'
but it doesn't matter.
Aug 17 '07 #9
bimbam
19 New Member
Yep, that's all correct as far as I can see now.

kind regards,

Jos

edit: you could've done 'calloc(n, sizeof(struct_p*))' instead of malloc(n*sizeof(struct_p*))'
but it doesn't matter.

Thanks.

To 'desallocate', is 'free(p)' enough? Or do I have to do everything in reverse to free each field of all the structures, free the structures abd then free the array?
Regards
Aug 17 '07 #10
JosAH
11,448 Recognized Expert MVP
Thanks.

To 'desallocate', is 'free(p)' enough? Or do I have to do everything in reverse to free each field of all the structures, free the structures abd then free the array?
Regards
Yep, you have to do it all yourself. C's free() function is quite stupid: it frees
the memory directly pointed to by its parameter and that's about it. It doesn't
know that that memory contains pointers to other 'things' that should be free'd
as well. You need C++ if you want that type of functionality. Or you can build
a nice little function for exactly this purpose.

kind regards,

Jos
Aug 17 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4458
by: Berk Birand | last post by:
Hi all, I have to use C-style structures for an assignement. I cannot have any methods or constructors for it. What has surprised me is that in my code, I have to allocate memory for an array of...
11
3362
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
8
3665
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
3739
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
11
3026
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
19035
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
4
5052
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
7945
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
2946
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
0
7093
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
7291
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,...
1
7012
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
5598
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,...
0
3180
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...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
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 ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
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...

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.