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.
CodePudding user response:
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]);
// ^^ ^^
}
CodePudding user response:
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]);