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 : -
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
typedef struct
-
{
-
int x;
-
int y;
-
}struct_p;
-
-
-
int main()
-
{
-
int i,n=2;
-
struct_p *p;
-
p=calloc(n,sizeof(struct_p*));
-
for (i=0;i<n;i++)
-
{
-
p[i]->x=0;
-
p[i]->y=1;
-
}
-
}
-
-
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
10 7484
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 : -
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
typedef struct
-
{
-
int x;
-
int y;
-
}struct_p;
-
-
-
int main()
-
{
-
int i,n=2;
-
struct_p *p;
-
p=calloc(n,sizeof(struct_p*));
-
for (i=0;i<n;i++)
-
{
-
p[i]->x=0;
-
p[i]->y=1;
-
}
-
}
-
-
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
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
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 '->' ?
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
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
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
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? -
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
typedef struct
-
{
-
int x;
-
int *y;
-
}struct_p;
-
-
-
int main()
-
{
-
int i,j,n=3;
-
int x,y;
-
struct_p **p;
-
p=malloc(n*sizeof(struct_p*));
-
-
for (i=0;i<n;i++)
-
{
-
p[i]=malloc(sizeof(struct_p));
-
p[i]->x=0;
-
p[i]->y=malloc(n*sizeof(int));
-
}
-
-
for (i=0;i<n;i++)
-
{
-
printf("p[%d]->x=%d | ",i,p[i]->x);
-
for (j=0;j<n;j++)
-
printf("p[%d]->y[%d]=%d ",i,j,p[i]->y[j]);
-
printf("\n");
-
}
-
}
-
-
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
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.
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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;
}
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |