0

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.

6
  • 1
    Define "similar". :O Your code is very far from standard C. Commented Oct 2, 2019 at 20:56
  • 3
    The keyword struct needs 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. Commented Oct 2, 2019 at 20:59
  • 2
    If you want to use a struct outside the function its definition must be visible there. Therefore you can't define it inside the function. You can define a variable of this struct type inside the function and return it, though. Commented Oct 2, 2019 at 21:00
  • The curly brackets represent scope of visibility of symbols. So you can't expect something declared in an inner scope (the function) to be visible on the outside. Commented Oct 2, 2019 at 21:03
  • With the struct type defined local within getData what 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? Commented Oct 2, 2019 at 21:19

2 Answers 2

1

Your problem seems to be a confusion regarding the usage of the struct keyword. You don't do struct myData to declare a variable named myData that is of struct type, because there isn't really a struct type. What you do is struct myData <SOMETHING> to define <SOMETHING> as being a new data type named struct myData. You can then say struct myData dat;, thereby declaring that dat is a variable of type struct myData.

You're also demonstrating the same confusion at the top, where you have struct getData(void)... you're attempting to declare getData as a function returning a struct, but you'd really have to do something like struct myData getData(void) to declare a function returning type struct myData.

Sign up to request clarification or add additional context in comments.

Comments

0

I think the more standard way to write what you're after is this, using an independent definition of the struct, and then using a pointer for the function call. As written your code seems to have some scope issues (declaring a struct typedef inside a function, hmmm...) [Incorrect->] plus I don't believe structs can be passed around that way in C, I'm pretty sure you have to use pointers. [Edit oops this is incorrect! See comments]

#include<stdio.h>

typedef struct {
    int count1;
    int count2;
    } MyStruct;

void getData (MyStruct* myDataPtr) {
    myDataPtr->count1 = 5;    //note pointer access notation ->
    myDataPtr->count2 = 6;
};

int main(void) {
    MyStruct myData;
    MyStruct* sPtr = &myData;  //create pointer to struct, assign to myDaya
    getData(sPtr);            //pass pointer to function
    printf("count1: %i \n", myData.count1);
    printf("count2: %i \n", myData.count2);
}

Outputs:

count1: 5
count2: 6

4 Comments

"I don't believe structs can be passed around that way in C" - you're wrong. They can be passed around by value without issue. MyStruct getData() is completely viable, and in fact one common technique for returning an array by value from a function (i.e. bury it in a structure type).
structs can be passed just fine. Only issue is that if it's really big then that there could be a lot of data being copied.
Thanks, I had no idea for some reason... can you do that in C++ too? And you can bury an array in a struct and pass it about, too... I wonder if the scope issue is a problem then?
You can also bury an array in a struct and assign one of those things to another to get around the lack of array assignment. It fills your code up of long-winded and ugly constructs, but every once in a while it's worth it. (And given the extreamely long-winded nature of some of the c++ code I'm producing thiese days aryStruct.ary[i] doesn't seem so bad anymore.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.