I've tried a lot of simple examples of this and haven't gotten any to work.
My goal is to have a function which declares a struct internally, sets the values of the struct, and then returns the struct.
struct getData(void){
typedef struct{
int count1;
int count2;
} MyStruct;
MyStruct myData;
myData.count1 = 5;
myData.count2 = 6;
return myData;
};
int main(void) {
struct myData = getData()
printf("count1: %i", myData.count1);
printf("count2: %i", myData.count2);
}
Every example I've found does something similar to this, but for some reason it's not finding my struct called MyStruct. Exact error is:
error: expected identifier or ‘(’ before ‘void’
struct getData(void){
^~~~
The error I keep getting makes me think it doesn't like the struct inside the function.
structneeds to be followed by either the name of a previously declared structure type, or the specification of the sdtructure. You can't just declare a variable or function as returning an unknown type of structure.getDatawhat form of magic do you expect to use outside the function to access the members set within? Amore fundamental question: What are you really trying to accomplish? I ask because this seems very much an XY problem, where you have a problem, decided firmly on a solution, tried the solution, and upon failing, decided the problem is within your solution; not the original problem itself. So.. what are you really trying to accomplish here?