I have an issue with structures in C, I don't know exactly what is happening. Could you explain me where I am wrong and correct me?
I declared a structure like below
typedef struct
{
Fuel_Intrl_IDs Curr_Bar; /*enum variable*/
uint16_t Pres_Value;
uint16_t Prev_Value;
}Bar_Dync_Data;
I assigned values for all the 3 variables, then when I am accessing data in these variables, value in "Prev_value is always returning "0".
On reassigning the structure as follows, everything is working fine:
typedef struct
{
uint16_t Pres_Value;
Fuel_Intrl_IDs Curr_Bar; /*enum variable*/
uint16_t Prev_Value;
}Bar_Dync_Data;
Can you explain what is happening here?
Code for reproducing
#include <stdio.h>
#include <stdlib.h>
#include <stdint-gcc.h>
typedef enum /*Fuel Internal IDs*/
{
Fuel_Intrl_ID_End
}Fuel_Intrl_IDs;
typedef struct /*Structure for Storing BAR ON/OFF Values*/
{
Fuel_Intrl_IDs Curr_Bar;
uint16_t Pres_Value;
uint16_t Prev_Value;
}Bar_Dync_Data;
Bar_Dync_Data FBar_Dync_Data;
int main()
{
printf("Hello world!\n");
FBar_Dync_Data.Prev_Value = 255;
while(1)
{
printf("\n Enter Pres value ");
scanf("%d",&FBar_Dync_Data.Pres_Value);
printf("\n Present value: %d",FBar_Dync_Data.Pres_Value);
printf("\n Previous value: %d",FBar_Dync_Data.Prev_Value);
FBar_Dync_Data.Prev_Value = FBar_Dync_Data.Pres_Value;
// getch();
}
return 0;
}
Related
In C we cannot assign int variable = true;, where as the below code executes.
typedef struct mystruct {
int variable;
} mystruct_;
int main(void){
// Your code here!
mystruct_ st = {true};
printf("%i",st.variable);
}
Why true isn't valid value for int variable = true, but is valid for an int member in a struct?
Can someone please explain why?
In C we cannot assign int variable = true;
This is not correct. It compiles and run successfully. The following code compiles and runs perfectly on GCC.
#include <stdio.h>
#include <stdbool.h>
typedef struct mystruct {
int variable;
} mystruct_;
int main(void){
int variable = true;
mystruct_ st = {true};
printf("%i %i",st.variable, variable);
}
By the way, the stdbool.h contains the following:
#define true 1
#define false 0
That's why it should work with int.
define code :
#include <stdio.h>
#include <string.h>
typedef int count_t;
typedef struct ptid_t {
short index;
short ioffset;
unsigned char type;
unsigned char networkType;
} ptid_t;
typedef union ptid_lst {
count_t count;
ptid_t item[1];
} plist_t;
main code :
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.count = 0xABCDABCD;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
console output :
abcdabcd
ffffabcd
ffffabcd
I just trying to change struct first value 'count' but other variables are changed.
The change value is 'count' in plist_t. but, why index and ioffset variables are changed both?
Because of this situation, I try to get the variable's address and result :
0x80479f4
0x80479f4
0x80479f6
The 'count' variable and item[0] struct has same address. why occured this situation?
In oppsite case are same too.
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.item[0].index = 0xabcd;
test.item[0].ioffset = 0xabc0;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
console output:
abc0abcd
ffffabcd
ffffabc0
Because plist_t isn't a struct, it's a union
In C, each member of a union starts at the same memory address.
If you want to change them independently, simply convert plist_t into a struct instead:
typedef struct ptid_lst {
count_t count;
ptid_t item[1];
} plist_t;
I have a address to a nested struct problem that I haven't been able to solve for a while right now.
So I have a ModuleStruct that holds a sensorStruct.
No problem to pass the address to the ModuleStruct in main. But when I do want to pass the address to the inner struct "SensorStruct" so readVoltage function can store the values, It fails.
Been googling and checking StackOverflow without finding anything that works. I hope I just done something stupid so it's easy to get it going.
Will be super happy if anyone could point me in the right direction (*puufh ;) )
typedef struct
{
int32_t Voltage;
int32_t Current;
}SensorStruct;
typedef struct
{
SensorStruct SensorData;
}ModuleStruct;
int main(void)
{
ModuleStruct Module_Data;
Sensor_Collect(&Module_Data);
}
void Sensor_Collect(ModuleStruct *Module_Data)
{
/// Here is the problem, I do not know how to pass the address of the SensorStruct
ReadVoltage(Module_Data->SensorData);
}
void ReadVoltage(SensorStruct *Sensor_Data)
{
Sensor_Data->Voltage = 5;
Sensor_Data->Current = 3;
}
Code that works. Take note that Sensor_Collect function is defined after ReadVoltage function, so it knows it's type when it calls it. Always compile your code with all warnings turned on.
#include <stdio.h>
#include <stdint.h>
typedef struct {
int32_t Voltage;
int32_t Current;
} SensorStruct;
typedef struct {
SensorStruct SensorData;
} ModuleStruct;
void ReadVoltage(SensorStruct *Sensor_Data) {
Sensor_Data->Voltage = 5;
Sensor_Data->Current = 3;
}
void Sensor_Collect(ModuleStruct *Module_Data) {
ReadVoltage(&(Module_Data->SensorData));
}
int main(void) {
ModuleStruct Module_Data;
Sensor_Collect(&Module_Data);
printf("voltage: %d\n", Module_Data.SensorData.Voltage);
printf("current: %d\n", Module_Data.SensorData.Current);
return 0;
}
I wrote a code which produces the following error:
Thread 1: EXC_BAD_ACCESS (code=1, adress=0x7.....)
I want the program to link the countries, states, cities and shops within an structure. But when I try to run my program it gives me the error you see above.
I already tried deleting the strcpy and the for but the error still occurs. So the error must be within the structures. What is it I'm doing wrong?
#include <stdio.h>
#include <string.h>
#define SMAX 16
#define CMAX 256
#define SHMAX 300
int main() {
struct country {
char cname[50];
struct state {
char sname[50];
struct city {
char cityname[50];
struct shop {
char shopname[50];
int countshop;
} shop[SHMAX];
int countcity;
} city[CMAX];
int countstate;
} state[SMAX];
} country;
// country = Germany;
strcpy(country.state[0].sname, "bayern");
strcpy(country.state[1].sname, "berlin");
strcpy(country.state[0].city[0].cityname, "ingolstadt");
strcpy(country.state[0].city[0].shop[0].shopname, "westpark");
strcpy(country.state[0].city[0].shop[1].shopname, "edeka");
for (int i = 0; i < SHMAX; i++) {
printf("%s\n", country.state[0].city[0].shop[i].shopname);
}
return 0;
}
The size of the struct is 69043124 bytes which is too much to fit on the stack.
As thread safety is no concern, the struct could be made static:
int main(void) {
static struct country {
I am trying to get multiple entries for my struct via the keyboard. I think I am wrong inside of scanf but I am not sure where I am wrong. Thanks!
Here is what I have:
#include <stdio.h>
#include <math.h>
int main()
{
//define the structure
struct course
{
char title[20];
int num;
} ;
// end structure define
//define the variable
struct course classes;
printf("Enter a course title and course number");
scanf("%s %d", classes[3].title, &classes.num);
return 0;
}
Fix the code as Carl said and it works fine:
#include <stdio.h>
int main()
{
struct course
{
char title[20];
int num;
} ;
struct course class;
printf("Enter a course title and course number");
scanf("%s %d", class.title, &class.num);
printf("%s %d", class.title, class.num);
return 0;
}
There are a couple issues.
You have this struct called "classes", but it has only 1 entry - you're accessing the 3rd entry so you're running off the end.
Also, Title is 20 bytes long, but if you enter something larger in scanf(), it will just overflow. The basic scanf() structure looks ok though.
I would set it up more like this:
#include <stdio.h>
#include <math.h>
#define MAX_CLASSES 50 // how big our array of class structures is
int main()
{
//define the structure
struct course
{
char title[20];
int num;
} ;
// end structure define
//define the variable
struct course classes[MAX_CLASSES];
int nCurClass = 0; // index of the class we're entering
bool bEnterMore = true;
while (bEnterMore == true)
{
printf("Enter a course title and course number");
scanf("%s %d", classes[nCurClass].title, &classes[nCurClass].num);
// if user enters -1 or we fill up the table, quit
if (classes[nCurClass].num == -1 || nCurClass > MAX_CLASSES-1)
bEnterMore = false;
}
}
That's the basic idea. Another improvement that could be made would be checking the length of the course title before assigning it to classes[].title. But you need something to do ;-)