how to use a struct pointer to get a struct pointer array's member
-
typedef struct {
int s1;
int s2;
int s3
}strTst;strTst spst1 = {3,5,7};
strTst spst2 = {1,2,4};
strTst *Sp1[2] =
{
&spst1,
&spst2
};strTst *pt = &Sp1;
I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1
-
typedef struct {
int s1;
int s2;
int s3
}strTst;strTst spst1 = {3,5,7};
strTst spst2 = {1,2,4};
strTst *Sp1[2] =
{
&spst1,
&spst2
};strTst *pt = &Sp1;
I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1
The last line won't compile:
Sp1
is an array of 2 pointers tostrTst
instances, not astrTst*
. To accesss1
inspst2
, you needstrTst *pt = Sp1[1];
pt->s1 = 0;Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
The last line won't compile:
Sp1
is an array of 2 pointers tostrTst
instances, not astrTst*
. To accesss1
inspst2
, you needstrTst *pt = Sp1[1];
pt->s1 = 0;Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
The last line won't compile:
Sp1
is an array of 2 pointers tostrTst
instances, not astrTst*
. To accesss1
inspst2
, you needstrTst *pt = Sp1[1];
pt->s1 = 0;Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Can I cast the strTst* pt to an array of 2 pointers to strTst instance? with
strTst *pt = &Sp1;
although, there is a warning compiler issued. I get the address of the Sp1 array. I think since i get the address of a data object,then i can operate on the data object.
-
Can I cast the strTst* pt to an array of 2 pointers to strTst instance? with
strTst *pt = &Sp1;
although, there is a warning compiler issued. I get the address of the Sp1 array. I think since i get the address of a data object,then i can operate on the data object.
A cast should only be used when necessary, which isn't the case here. EDIT: I'm compiling under C++, so maybe it's an error there, and only a warning in C.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
typedef struct {
int s1;
int s2;
int s3
}strTst;strTst spst1 = {3,5,7};
strTst spst2 = {1,2,4};
strTst *Sp1[2] =
{
&spst1,
&spst2
};strTst *pt = &Sp1;
I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1
How about this:
typedef struct {
int s1;
int s2;
int s3;
}strTst;strTst spst1 = { 3,5,7 };
strTst spst2 = { 1,2,4 };
strTst* Sp1[2] =
{
&spst1,
&spst2
};strTst** pt = Sp1;
void f () {
pt[1]->s1 = 0;
}Mircea
-
typedef struct {
int s1;
int s2;
int s3
}strTst;strTst spst1 = {3,5,7};
strTst spst2 = {1,2,4};
strTst *Sp1[2] =
{
&spst1,
&spst2
};strTst *pt = &Sp1;
I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1
You have declared
Sp1
to be an array of pointers, each of which points to astrTst
structure. So to get one of the pointers you just need normal array addressing, e.g.Sp1[1]
returns the second element of the array. So all you need is:strTst *pt = Sp1[1]; // pt now equals &spst2
int value = pt->s2; // get the value of spst2.s2 -
typedef struct {
int s1;
int s2;
int s3
}strTst;strTst spst1 = {3,5,7};
strTst spst2 = {1,2,4};
strTst *Sp1[2] =
{
&spst1,
&spst2
};strTst *pt = &Sp1;
I wonder how can i use pt to get a value in spst2, I checked, (*pt) value = &spst1, *(pt+1) = &spst2. but the compiler not let me to use (*pt)->s1, or ( (strTst *)(*pt) )->s1
strTst *pt = Sp1[0];
pt->s1; // is spst1.s1
pt->s2; // is spst1.s2
pt->s3; // is spst1.s3pt = Sp1[1];
pt->s1; // is spst2.s1
pt->s2; // is spst2.s2
pt->s3; // is spst2.s3// alternative
strTst **ppt = Sp1;
ppt[0]->s1; // is spst1.s1
ppt[0]->s2; // is spst1.s2
ppt[0]->s3; // is spst1.s3ppt[1]->s1; // is spst2.s1
ppt[1]->s2; // is spst2.s2
ppt[1]->s3; // is spst2.s3