473,569 Members | 2,526 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 7491
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,siz eof(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,siz eof(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

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

Similar topics

8
4463
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 structures with malloc. Otherwise, I get a seg fault. Here's the code: struct Employee { char* name; // Pointer to character string holding name...
11
3372
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 don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of...
8
3669
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with...
5
3742
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: ...
11
3030
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
19045
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
4
5055
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each...
1
7953
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to...
3
2982
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 facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various “nested structures”...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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
7676
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
7974
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...
1
5513
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...
0
5219
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...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.