problems with pointer to struct
-
Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.
typedef enum
{
NORMAL,
WARNING,
}WATER_TEMP;typedef struct
{
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
}Engine_t;typedef struct
{
Engine_t* engine;
ABS_t* abs;
// ... other members
}ECU_t;void ECU_DoTask(ECU_t* output)
{
output->engine->water_temp = NORMAL;}
void main ()
{
ECU_t ecu;
ECU_DoTask(&ecu);
// i want to access to water_temp. how can i do it?
// for example: ecu.engine->water_temp ????
while(1);
} -
Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.
typedef enum
{
NORMAL,
WARNING,
}WATER_TEMP;typedef struct
{
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
}Engine_t;typedef struct
{
Engine_t* engine;
ABS_t* abs;
// ... other members
}ECU_t;void ECU_DoTask(ECU_t* output)
{
output->engine->water_temp = NORMAL;}
void main ()
{
ECU_t ecu;
ECU_DoTask(&ecu);
// i want to access to water_temp. how can i do it?
// for example: ecu.engine->water_temp ????
while(1);
}your remarks show the correct way to access the variable, but (and it is a big 'but') the memory for the pointed struct must be first allocated. try
int main ()
{
ECU_t ecu;
ecu.engine = (Engine_t *) malloc(sizeof(ecu.engine)); // allocate memory fot the Engine_t struct
ECU_DoTask(&ecu);
printf("water_temp = %d\n", ecu.engine->water_temp);
free( ecu.engine); // release allocated dynamic memoryreturn 0;
} -
Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.
typedef enum
{
NORMAL,
WARNING,
}WATER_TEMP;typedef struct
{
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
}Engine_t;typedef struct
{
Engine_t* engine;
ABS_t* abs;
// ... other members
}ECU_t;void ECU_DoTask(ECU_t* output)
{
output->engine->water_temp = NORMAL;}
void main ()
{
ECU_t ecu;
ECU_DoTask(&ecu);
// i want to access to water_temp. how can i do it?
// for example: ecu.engine->water_temp ????
while(1);
}Each structure inside is a pointer and needs to be allocated and even worse this is inherently dangerous because any of those pointers could be NULL if an error occurred and that line will crash.
output->engine->water_temp = NORMAL;
What you are building is a database and as the entries are small there is no advantages to allocation and pointers, you only do pointers if the data is going to be large. Just place the structure in as is.
typedef enum
{
NORMAL = 0,
WARNING = 1,
} WATER_TEMP;typedef struct {
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
} Engine_t;typedef struct
{
Engine_t engine; // <== Not a pointer anymore
// ... other members
} ECU_t;int main(void)
{
/* This creates a test instance .. requires C99 or higher wont work on old C89 compiler */
ECU_t ecu_test = { .engine.water_temp = NORMAL,
.engine.speed = 10 };/\* You access them in the normal way \*/ printf("Water temp is %i\\n", ecu\_test.engine.water\_temp); printf("Engine speed is %i\\n", (unsigned int)ecu\_test.engine.speed);
}
In vino veritas
-
Each structure inside is a pointer and needs to be allocated and even worse this is inherently dangerous because any of those pointers could be NULL if an error occurred and that line will crash.
output->engine->water_temp = NORMAL;
What you are building is a database and as the entries are small there is no advantages to allocation and pointers, you only do pointers if the data is going to be large. Just place the structure in as is.
typedef enum
{
NORMAL = 0,
WARNING = 1,
} WATER_TEMP;typedef struct {
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
} Engine_t;typedef struct
{
Engine_t engine; // <== Not a pointer anymore
// ... other members
} ECU_t;int main(void)
{
/* This creates a test instance .. requires C99 or higher wont work on old C89 compiler */
ECU_t ecu_test = { .engine.water_temp = NORMAL,
.engine.speed = 10 };/\* You access them in the normal way \*/ printf("Water temp is %i\\n", ecu\_test.engine.water\_temp); printf("Engine speed is %i\\n", (unsigned int)ecu\_test.engine.speed);
}
In vino veritas
thank you. but i want to create a function with form of:
void ECU_DoTask(ECU_t* out_ecu)
{
// perform out_ecu->engine->speed = ...
// perform out_ecu->abs->status = ...
// perform out_ecu->bcm->high_beam_status = ...
}because i should pass the values of ecu parameters as an output argument of function. so i need use pointers.
-
thank you. but i want to create a function with form of:
void ECU_DoTask(ECU_t* out_ecu)
{
// perform out_ecu->engine->speed = ...
// perform out_ecu->abs->status = ...
// perform out_ecu->bcm->high_beam_status = ...
}because i should pass the values of ecu parameters as an output argument of function. so i need use pointers.
You are missing the point nothing I have done stops you using pointers on the interface :-) In my code above you could do
/* This is called a static allocation */
/* You use the & symbol to make a pointer to the allocation */
static ECU_t ecu_test;
ECU_DoTask(&ecu_test);Or you could do this
/* This is called a dynamic allocation */
/* malloc makes space for the structure on the heap and gives you a pointer */
ECU_t* ecu_test = malloc(sizeof(ECU_t));
ECU_DoTask(ecu_test);However none of that has anything remotely to do with what is in the structure and removing the pointers and internally you have no way to know how I did the allocation externally :-) I think what has thrown you is me creating one of your structures without having to create it like you want, but that is just me being tricky with C literals to give you something to test.
In vino veritas
-
Hi every one. I have typical structure (ECU_t) which has members that considered to be pointer to struct. i make my question in comments. please help me. by the way i have used freescale code warrior as compiler.
typedef enum
{
NORMAL,
WARNING,
}WATER_TEMP;typedef struct
{
WATER_TEMP water_temp;
uint16_t speed;
// ... other members
}Engine_t;typedef struct
{
Engine_t* engine;
ABS_t* abs;
// ... other members
}ECU_t;void ECU_DoTask(ECU_t* output)
{
output->engine->water_temp = NORMAL;}
void main ()
{
ECU_t ecu;
ECU_DoTask(&ecu);
// i want to access to water_temp. how can i do it?
// for example: ecu.engine->water_temp ????
while(1);
}Why bother making Engine_t and ABS_t pointers? Just make them members.