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.
Related
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;
}
//Program to understand Nested structs in C
#include <stdio.h>
typedef union test {
float tet;
struct {
int bite;
} p;
} U_test;
//union test U_test;
struct U_test.p = {.bite=150};
int main()
{
printf("%d\n", U_test.p.bite);
U_test.tet = 0.0;
printf("%d", U_test.p.bite);
return 0;
}
Here is the error code I keep seeing below. Not sure I am doing something wrong here or am I typing something wrong?Please advise. Thanks in advance.
error: expected identifier or ‘(’ before ‘.’ token
struct U_test.p = {.bite=150};
struct U_test.p = {.bite=150};
That is not the correct syntax to create a variable of the defined union type. It should be:
U_test u_test = { .p.bite = 150 };
After making that change all the references in main which are currently U_test need to be changed to u_test.
Here is the full program corrected:
#include <stdio.h>
typedef union test{
float tet;
struct{
int bite;
}p;
}U_test;
//union test U_test;
U_test u_test = { .p.bite=150 };
int main()
{
printf("%d\n", u_test.p.bite);
u_test.tet = 0.0;
printf("%d", u_test.p.bite);
return 0;
}
I have learned how to use functions and structs and pointers. I want to combined them all into one. But the code that I write doesn't seem to work. The compiler tells me the test is an undeclared identifier. Here is the code:
#include <stdio.h>
#include <stdlib.h>
struct character
{
int *power;
};
void test (use_power)
int main ()
{
test (use_power)
printf("%d\n",*power);
return 0;
}
void test ()
{
int use_power = 25;
struct character a;
a.power = &use_power;
}
Your code has many mistakes it can't even compile
Multiple missing semicolons.
Implicit declaration of test() here
test (use_power)
with a missing semicolon too.
power is not declared in main().
This line
void test use_power()
does not make sense and is invalid, and also has no semicolon.
The a instance in test() defined at the end is local to test() and as such will be deallocated when test() returns. The use_power int, has exactly the same problem and trying to extract it's address from the function is useless because you can't access it after the function has returned.
I have no idea what you were trying to do, but this might be?
#include <stdio.h>
#include <stdlib.h>
struct character {
int *power;
};
/* Decalre the function here, before calling it
* or perhaps move the definition here
*/
void test(struct character *pointer);
/* ^ please */
int
main(void) /* int main() is not really a valid signature */
{
struct character instance;
test(&instance);
if (instance.power == NULL)
return -1;
printf("%d\n", *instance.power);
free(instance.power);
return 0;
}
void
test(struct character *pointer)
{
pointer->power = malloc(sizeof(*pointer->power));
if (pointer->power != NULL)
*pointer->power = 25;
}
Your code seems to be wrong. Your definition for test contains no arguments as
void test ()
{
int use_power = 25;
struct character a;
a.power = &use_power;
}
but your prototype contains one argument
void test (use_power)
which is wrongly put. First there are no semicolons; at the end of your prototype declaration, secondly by looking at your code, use_power is a variable and not a datatype so it cannot be present solely in a function declaration.
You will get an argument mismatch error.
You have used the line in main()
printf("%d\n",*power);
which is absolutely wrong. you cannot access any member of a structure without a structure variable.
And again, you have not mentioned the; after your call to the incorrect test()before this line
As you have not put your question so properly, I must figure out what you wish to achieve. I bet you want to hold the address of a integer in the pointer member of a structure and then print its value.
Below is a code snippet which will work as you desire.
#include <stdio.h>
#include <stdlib.h>
struct character
{
int *power;
};
struct character a; //define a structure variable
void test ();
int main ()
{
test ();
printf("%d\n",*(a.power)); // print the member of structure variable a
return 0;
}
void test ()
{
int use_power = 25;
a.power = &use_power;
}
example
#include <stdio.h>
struct character {
int *power;
};
void test(struct character *var);
int main (void){
struct character use_power;
int power = 5;
use_power.power = &power;
test(&use_power);
printf("%d\n", power);
return 0;
}
void test(struct character *var){
int use_power = *var->power;
*var->power = use_power * use_power;
}
I am trying to declare a struct and use it in multiple files and I am getting an error that I cannot figure out. Sample code is posted below.
in test.h
#ifndef TEST_H
#define TEST_H
struct mystruct;
struct mystruct *new_mystruct();
void myprint(struct mystruct*,int);
#endif
int test.c
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
struct mystruct {
int *myarray;
};
struct mystruct *new_mystruct(int length)
{
int i;
struct mystruct *s;
s = malloc(sizeof(struct mystruct));
s->myarray = malloc(length*sizeof(int));
for(i = 0; i < length; ++i)
s->myarray = 2*i;
return s;
}
in main.c
#include "test.h"
#include <stdio.h>
int main()
{
int len = 10;
struct mystruct *c = new_mystruct(len);
myprint(c, len);
printf("%f", c->myarray[3]); // error: dereferencing pointer to incomplete type
return 0;
myprint() prints out 0 2 4 6 8 10 12 14 16 18. why doesn't the myprint(function work but the printf statement doesn't? why is it ok to pass it into a function but not use it in main? Thanks.
Currently main() only knows that struct mystruct is a type, but it doesn't know anything about its internal structure, because you've hidden it in test.c.
So you need to move this definition:
struct mystruct {
int *myarray;
};
from test.c to test.h, so that it's visible to main().
Note: what you're doing here is a classic example of an opaque type. This can be a very useful technique when you want to hide implementation details from code that is going to be calling your API.
Main.c doesn't know the contents of the mystruct structure. Try moving these lines:
struct mystruct {
int *myarray;
};
from test.c to test.h.
While you're at it, I think you mean "int myarray" not "int *myarray".
I created a structure and wanted to assign the values to a Function Pointer of another structure. The sample code I wrote is like below. Please see what else I've missed.
#include <stdio.h>
#include <string.h>
struct PClass{
void *Funt;
}gpclass;
struct StrFu stringfunc;
struct StrFu{
int a ;
char c;
};
Initialise(){
}
main()
{
stringfunc.a = 5;
stringfunc.c = 'd';
gpclass.Funt = malloc(sizeof(struct StrFu));
gpclass.Funt = &stringfunc;
memcpy(gpclass.Funt,&stringfunc,sizeof(struct StrFu));
printf("%u %u",gpclass.Funt->a,gpclass.Funt->c);
}
There are several problems:
A function pointer is not the same as void *, in fact you cannot rely on being able to convert between them.
You shouldn't cast the return value of malloc() in C.
You shouldn't call malloc(), then overwrite the returned pointer.
You don't need to use malloc() to store a single pointer, just use a pointer.
You shouldn't use memcpy() to copy structures, just use assignment.
There are two valid main() prototypes: int main(void) and int main(int argc, char *argv[]), and you're not using either.
there is lots of problem in your code , I try to correct it ,hope it will help
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct PClass{
void *Funt;
}gpclass;
struct StrFu{
int a ;
char c;
};
struct StrFu stringfunc;
int main()
{
stringfunc.a = 5;
stringfunc.c = 'd';
gpclass.Funt = malloc(sizeof(struct StrFu));
gpclass.Funt = &stringfunc;
memcpy(gpclass.Funt,&stringfunc,sizeof(struct StrFu));
printf("%d %c",((struct StrFu*)gpclass.Funt)->a,((struct StrFu*)gpclass.Funt)->c);
return 0;
}
it outputs
5 d