I have a school project where I have to make my structures and functions in a .h header file.
I've created my structure but cannot use any of the variables within it as whenever I call it it highlights the structure name and tells me its not defined even though it clearly is in my structure and doesn't highlight or give me any syntax errors.
#include <stdio.h>
typedef struct test1 {
int array1[3];
int array2[3];
};
int main(void) {
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
}
I have tried using typedef with and without and its the same result. if I create individual variables outside the structure I get no issues so i believe its some issue with how I'm creating my structures but I don't know what the issue is.
The use of typedef makes me think that you actually want to define a type named test1. Move the name to after the struct:
#include <stdio.h>
typedef struct {
int array1[3];
int array2[3];
} test1; // now a name you can use
You then need to create an instance of test1 to be able to use it with scanf_s:
int main(void) {
test1 t1; // `t1` is now a `test1` instance
scanf_s(" %d %d", &t1.array1[1], &t1.array2[1]);
// ^^ ^^
}
You declared type specifier struct test1 (and moreover the typedef declaration does not even declare a typedef name for the type specifier struct test1)
typedef struct test1 {
int array1[3];
int array2[3];
};
But the call of scanf_s
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
expects objects. test1 is not an object. This name is not even declared.
You could write for example
struct test1 test1;
scanf_s(" %d %d", &test1.array1[1], &test1.array2[1]);
Related
I am trying to create a struct that I will use in a function via pointers. The issue is that I do not want to use global variables therefore I can't use a pointer to a struct as a parameter for the function prototype if I try to define the struct in main file, since it has not been defined yet.
How would I go about doing this? What I think the solution is, is to define the struct in a header file, then create local variables of that type in the main file. Is this the right way to go about this? Would appreciate some info about what i'm actually doing here if this is correct.
Sorry if I did anything wrong when posting, Its my first time.
Example of what I am thinking the solution is
Main.h
#include <stdio.h>
typedef struct Vehicle{
int a;
char b;
};
function(Vehicle *p);
Main.c
#include "Main.h"
Vehicle Car1;
Vehicle *p=&Car1;
function(p);
The proper syntax for a typedef is
typedef T-IDENTIFIER IDENTIFIER-LIST;
wherein the comma separated identifiers listed in IDENTIFIER-LIST become aliases for T-IDENTIFIER. A lot of the time IDENTIFIER-LIST will consist of a single identifier.
For example, in
typedef int integer, number;
integer and number are now type aliases for int.
When it comes to using typedef with structs, the form
typedef struct foo { /* ... */ } foo_type;
is more or less shorthand for
typedef struct foo foo_type;
struct foo { /* ... */ };
but does allow you to typedef an anonymous struct
typedef struct { /* ... */ } foo_type;
With all that said, in your code you have omitted the IDENTIFIER-LIST from your typedef.
If main.c really does consist entirely of the code you've posted, it will not compile. Every C program needs an entry point, and in a hosted environment that is the function main with the signature int main(void) or int main(int argc, char **argv).
While you can declare variables outside of functions (i.e., globals), you can not call functions from outside of functions. Everything starts from main.
A working example program:
main.h:
typedef struct {
int a;
char b;
} Vehicle;
void function(Vehicle *p);
main.c:
#include <stdio.h>
#include "main.h"
int main(void) {
Vehicle car = { 51, 'A' };
function(&car);
}
void function(Vehicle *v) {
printf("Vehicle: a: %d, b: %c\n", v->a, v->b);
}
I can't use the struct as a parameter for the function prototype
You misunderstood something.
Your typedef is rather useless.
You of course can use pointers to structs as function parameters and in the function prototypes.
typedef struct {
int a;
char b;
} Vehicle;
int foo(Vehicle *); // prototype
You can't call function not outside other functions (as it is shown in the main.c
I seem to have run into a chicken and egg problem.
I want to have a structure that as one of its members is a function pointer. However this function pointer wants to use that same structure as it's argument. This creates an issue where I have to define the function pointer before I can include it as a member, but I can't properly define it until I've defined the structure.
I have found that if I simply leave the argument list for the function pointer blank it SEEMS to work, though what I have read is that this is potentially fraught with issues.
Below is what I currently have:
#include <stdio.h>
typedef void (*IO_fun_ptr_t)();
typedef struct IO_config_t{
int address;
IO_fun_ptr_t IO_fun_ptr; //pointer to the function to be used
} IO_config_t;
void print_address (IO_config_t *input){
printf("The address is %d \n", input->address);
printf("Push any key to continue:");
getchar();
}
void main()
{
IO_config_t input = {.address = 16,
.IO_fun_ptr = &print_address};
input.IO_fun_ptr(&input);
}
The result is:
The address is 16
Push any key to continue:
This works but I'm concerned about the potential implications of leaving that argument blank.
As a bit of an aside, I originally thought that I should be able to use void* as an argument as a placeholder for pointer to an unknown argument type, but I would get compile errors when doing so at the point where I assign the pointer to my function:
typedef void (*IO_fun_ptr_t)(void *);
(Error[Pe144]: a value of type "void (*)(IO_config_t *)" cannot be
used to initialize an entity of type "IO_fun_ptr_t")
Any advice on how to do this better and cleaner?
Use forward-declarations.
This is a way of stating that a structure exists, but without providing details of all the members of the structure until later.
#include <stdio.h>
// 1.) Forward declaration: Here is the name of the structure
// but member-details are omitted.
struct IO_config_t;
// 2.) typedef of the structure
// Still no details on the members.
typedef struct IO_config_t IO_config_t;
// 3.) The parameter to the function is listed, using the definition
// from step 2.) (note: Still no details on the members yet)
typedef void (*IO_fun_ptr_t)(IO_config_t* input);
// 4.) Now we actually detail the members of the structure
struct IO_config_t{
int address;
IO_fun_ptr_t IO_fun_ptr;
};
void print_address (IO_config_t *input){
printf("The address is %d \n", input->address);
printf("Push any key to continue:");
getchar();
}
void main()
{
IO_config_t input = {.address = 16,
.IO_fun_ptr = &print_address};
input.IO_fun_ptr(&input);
}
This is demonstrated in the short program: https://ideone.com/p3jBYt
So I had searched through stack exchange and couldn't find anything so humbled myself to asking a question. Just as I'm getting to the end of writing everything up, I glance over to the "Similar Questions" box to the right, and I happen to see the following question that I didn't come across before:
How to properly define a function pointer in struct, which takes struct as a pointer?
And in its answer I found my answer. I simply have to define the function pointer in the structure itself, not before hand. (I had tried, but forgot to include the struct keyword in the definition so it didn't work since the type def wasn't complete I am guessing).
Below is what compiles clean and seems to work:
#include <stdio.h>
typedef struct IO_config_t{
int address;
void (*IO_fun_ptr)(struct IO_config_t *); //pointer to the function to be used
} IO_config_t;
void print_address (IO_config_t *input){
printf("The address is %d \n", input->address);
printf("Push any key to continue:");
getchar();
}
void main()
{
IO_config_t input = {.address = 16,
.IO_fun_ptr = &print_address};
input.IO_fun_ptr(&input);
}
The following code compiles and runs fine:
#include <stdio.h>
typedef int Someint;
typedef int Someint;
int main()
{
Someint b = 4;
printf("%d", b);
return 0;
}
The following code doesn't compile. It's giving me an error conflicting types for 'Somestruct'.
#include <stdio.h>
typedef struct
{
int x;
}
Somestruct;
typedef struct
{
int x;
}
Somestruct;
int main()
{
Somestruct b;
b.x = 4;
printf("%d", b.x);
return 0;
}
Why can I typedef one type (int in first code) twice without error ,but the same thing fails for another type (the structure above)? What is the difference between the two cases?
I'm using the MinGW compiler that came with CodeBlocks 12.11.
The thing is that when you do:
typedef struct
{
} Somestruct;
It creates an anonymous struct - you can expect some hidden implementation-defined guaranteed-unique placeholder identifier to be used - for which you specify the typedef. So, when you do it twice you get a conflict in having the same typedef-ed name asked to refer to two distinct structures. With int, you're simply repeating the original. If you give the struct an actual name then that lets you repeat the typedef:
typedef struct Somestruct
{
} Somestruct;
Because you're defining your typedef using an anonymous struct, both definitions are distinct.
The following doesn't do this, and works. (note that you can still only define the struct once)
#include <stdio.h>
typedef struct foo
{
int x;
}
Somestruct;
typedef struct foo Somestruct;
#include <stdio.h>
#include <conio.h>
typedef arrChoice[10] /*is this a global variable?*/
int main() {};
getch();
return 0;
}
its not done yet, but this is what i meant.
typedef is not a global variable, it's simply an alias for another type. I usually use them for function pointers when I'm passing those around because writing them out every time is annoying.
typedef int (*function)(int, int);
I also use them to define a structure, union, or enumeration as a type
typedef struct {
int x;
int y;
int z;
} Point;
typedef declares the new type not variable.
This might help you. In the code you posted here, there is a error. There are no statements in side main function. getch and return statements should be inside main function. I feel your code should be like this.
#include <stdio.h>
typedef int arrChoice; /* arrChoice is alias to int */
arrChoice a[10] ;/* array a is a global variable of integers*/
int main()
{
getch();
return 0;
}
please note that the purpose of typedef is to assign alternative names to existing types(int,float,double,etc.). The following statements are similar.
typedef arrChoice[10] is similar to typedef int[10];
When you try to refer arrChoice, then you get an error message
expected expression before 'arrChoice'.
#include <stdio.h>
main()
{
typedef struct{
char *name;
int age;
}person[5];
int i;
for (i=0;i<5;i++){
printf ("name:");
scanf("%s",person[i].name);
printf("\nage:");
scanf("%d",&person[i].age);}
for (i=0;i<5;i++){
printf ("person:%d",i);
printf ("name:%s",person[i].name);
printf ("age:%d",person[i].age);
}
}
this is the sample program i have. But while compiling i keep getting the error "expected expression before person in line 10,12,16 and 17? What am i doing wrong?
To fix the syntax error, remove the typedef keyword (you're trying to declare a variable, not a type).
Better yet, change to:
typedef struct{
char *name;
int age;} Person;
Person person[5];
Also, the following is wrong:
scanf("%s",person[i].name);
You need to first allocate memory for person[i].name (for example, using malloc()).
Lastly, the %s format specifier in the following line is not correct:
printf ("age:%s",person[i].age);
person is a type, not an object. You cannot "scanf() into a type: person".
I'd simply remove the typedef and just leave the struct definition outside the body of main; and create an object inside
struct person { /* ... */ };
int main(void) {
struct person person[5];
/* ... */
return 0;
}