Trying to compile a function with pointer to structure as return value with gcc

In summary, there was an issue with creating functions with return values pointers to structures in gcc. The problem was that semi-colons were missing after the structure declarations, which was causing the error. It was suggested to use typedef before the structure definition or to implement it using tags.
  • #1
Werg22
1,431
1
I get an error with gcc "error: two or more data types in declaration specifiers" when trying to compile something of the form:

struct tag *function (...) { ...}

Apparently it won't let me create functions with return values pointers to structures. I know that it's perfectly valid code, so I don't know why it's not letting me do this. Any help on this would be appreciated.

edit:

The problem seem to have been that I didn't put semi-colons after the structure declarations. The prof mislead me into error.
 
Last edited:
Technology news on Phys.org
  • #2
Yes, you would need a semicolon after the struct definition statement.
It would also help if you could post an abridged code the demonstrates the problem, and the associated error codes.
Try typedef before your structure definition, so you can declare directly
tag *function(...){...}
just like any other type, if that solves your problem.
 
  • #3
I needed to implement it by using tags rather than typedef. But thanks for the answer anyways.
 

Related to Trying to compile a function with pointer to structure as return value with gcc

1. How do I declare a function with a pointer to a structure as its return value in GCC?

To declare a function with a pointer to a structure as its return value in GCC, use the following syntax:
struct myStruct * myFunction(void)
This will declare a function named myFunction that returns a pointer to a structure of type myStruct.

2. Can I pass a pointer to a structure as an argument to a function in GCC?

Yes, you can pass a pointer to a structure as an argument to a function in GCC. Simply use the pointer as you would any other variable in the function's parameter list. For example:
void myFunction(struct myStruct * myStructPointer)
This will declare a function named myFunction that takes a pointer to a structure of type myStruct as an argument.

3. How do I access the members of a structure that is returned by a function in GCC?

To access the members of a structure that is returned by a function in GCC, use the arrow operator (->) on the function call. For example:
myFunction()->member1
This will access the member1 variable of the structure returned by the myFunction function.

4. Can I return a pointer to a structure that is dynamically allocated in GCC?

Yes, you can return a pointer to a structure that is dynamically allocated in GCC. Just make sure to allocate the memory for the structure using malloc or calloc before returning it from the function.

5. How do I free the memory allocated for a structure that is returned by a function in GCC?

To free the memory allocated for a structure that is returned by a function in GCC, use the free function on the pointer to the structure. For example:
free(myFunction())
This will free the memory allocated for the structure that is returned by the myFunction function.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
958
  • Programming and Computer Science
Replies
1
Views
599
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
747
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top