d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Next Big C Question
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 9 2012 12:15pm
what is syntax for creating a structure with a pointer to another structure if these 2 structures are in the same complex structure.

Here is what I tried to do and the compiler is very mad about it. To be specific, it is expecting something before the "=" token.

Code
typedef struct  {

float max;

}maxHolder;

typedef struct  {

float min;

maxHolder maxFloat;

}minHolder;

typedef struct {

float a;
float b;
float c;
float d;
float e;

minHolder minFloat;

}maxStruct;

typedef struct {

float f;
float g;
float h;
float i;
float j;

maxStruct *maxGroup = malloc(1*sizeof(maxStruct);

}minStruct;

minStruct floatList;


I have also tried

Code
maxStruct maxGroup1;
maxStruct *maxGroup;
maxGroup = &maxGroup1;


and a lot of variations of that. I am just missing something here that I'd like help with please.

This post was edited by Nom_Nomz on Dec 9 2012 12:20pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 9 2012 12:31pm
just look at the traditional node example

Code
typedef struct node_s {
void *data;
struct node_s *next;
} NODE;


Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 9 2012 12:51pm
I think that is the first thing I did and for some reason I couldn't call the pointer maybe because I didn't know the syntax or something but it went like this

NODE.next.(some variable in this struct); and the compiler would tell me that it could not find the variable because next was not of type struct

I even tried NODE.(*next).(somevariable in this struct)

nothing worked so I went on to keep trying cause I can't find anything on this, not in our textbook or anywhere online.

I think I even tried to call the pointer itself like this

(*next).(some variable in this structure);

and that was also problematic >.<

This post was edited by Nom_Nomz on Dec 9 2012 12:54pm
Member
Posts: 2,425
Joined: Aug 17 2007
Gold: 3,754.50
Dec 9 2012 01:17pm
This is all wrong

Code
maxStruct *maxGroup = malloc(1*sizeof(maxStruct);

First off you're brackets don't match. Secondly you can't do assignment inside a structure definition. To make this compile you'd have to change it to:

Code
maxStruct maxGroup;

However, then you got nothing to do with pointers left at all.

Regarding syntax for accessing members inside a pointed-to object, you use the "->" operator. For example:

Code
typedef struct  {
   int a;
   int b;
} item;

typedef struct  {
   int key;
   item *data;
} map;

int main() {
   map info;
   info.data = malloc(sizeof(item));
   info.data->a = 3;
   info.data->b = 9;

   return 0;
}
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 9 2012 01:28pm
maxStruct maxGroup;

is what I've been using at the moment cause I couldn't get the pointers to work I will use the -> operator and see if it ixes my problem thanks!
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 9 2012 02:36pm
I think you can get away with the pointer inside the struct. I know for classes you have to initialize them in the constructor but I'd have to double check again for structs..

This post was edited by Eep on Dec 9 2012 02:37pm
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 9 2012 03:11pm
Code
info.data->b = 9;


that operator solved everything the program runs great!!

THANKS!
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Dec 9 2012 03:12pm
Quote (Eep @ 9 Dec 2012 16:36)
I think you can get away with the pointer inside the struct. I know for classes you have to initialize them in the constructor but I'd have to double check again for structs..


The pointer in the struct worked fine rom teh get go I just wasn't keen on the proper operator to access it >.<
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 9 2012 07:03pm
Quote (Nom_Nomz @ Dec 9 2012 04:12pm)
The pointer in the struct worked fine rom teh get go I just wasn't keen on the proper operator to access it >.<


oh lol I didn't understand the question, but yeah -> is what you want to use, and it is perfectly legal to do something like

objectA->objectB->data_fieldC;
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll