0

I'm new to C and I'm working with an opaque data structure passed by double reference. I've declared the struct prototype in cars.h as typedef struct car car. In cars.c I then go on to define the struct with the following members:

struct car{char model;
           int year; };

I have a function to set the year of the car:

void set_year(car **my_car , int year){
      *my_car->year = 1998;  
}

However, I can't see the members of the struct even though set_year and car are both defined in car.c. How do I access these members correctly?

*EDIT

Here is the resolution to what my question was asking:

void set_year(car **my_car , int year){
      (*my_car)->year = 1998;  
}
  • 1
    What do you mean by "opaque", exactly? You aren't using `void*` so how is it opaque? – Dai Aug 29 '20 at 04:31
  • Is there a reason you're doing `typedef struct car car`? I don't think that's a good idea if you're new to C. – Dai Aug 29 '20 at 04:31
  • I don't see any opaque pointers. Did you mean to say `void* my_care` there? – Tanveer Badar Aug 29 '20 at 04:33
  • Structs used across translation-units (i.e. multiple source files) should be defined in `.h` header files, not `.c` files. Only define "local" or "private" structs in `.c` files. – Dai Aug 29 '20 at 04:33
  • Your program as-is does not compile in GCC because `**car my_char` is not valid, even with the `typedef`. Please repost your program without using the `typedef`. Here: https://onlinegdb.com/B1eLoLDQw – Dai Aug 29 '20 at 04:35
  • If I change your program to not use `typedef` and use the correct syntax in `set_year` then it runs fine: https://onlinegdb.com/rJpqsUDmv – Dai Aug 29 '20 at 04:38
  • @Dai could you explain why they're not valid? This is just an example and as written, yes it won't compile. By opaque I mean that that the members of the data structure cannot be accessed outside of cars.c. – redengineer Aug 29 '20 at 04:38
  • @redengineer It's not valid syntax because the `*` (denoting a pointer) goes **on the right side** of the type-name (it should be `struct car** my_car`, not `**struct car my_car`). – Dai Aug 29 '20 at 04:40
  • @redengineer Opaque means that **zero** type information is available to the external consumer (i.e. your code isn't even aware it's `struct cars` - it would only see `void*`). Also note that C doesn't have any concept of access-modifiers (it doesn't have `public` or `private` members unlike C++, Java, C#, etc) so a `struct` in C cannot have members that "cannot be accessed" outside of `cars.c`, this is because all parts of a C program need to know the exact layout of a `struct`. The only way to implement "opaque" data in C is with a `void*` (in which case there's no `struct` at all). – Dai Aug 29 '20 at 04:41
  • @Dai, yes you are correct regarding the placement of the `*` in my example, my mistake there. I have it correct in the code, however. – redengineer Aug 29 '20 at 04:43
  • @redengineer What do you mean by "correct in the code" - what correct code? The code you've posted to your question is syntactically invalid. We can't help you unless you post your full, **real** code. – Dai Aug 29 '20 at 04:44
  • @Dai thanks for the help, I ended up figuring it out. I think I wasn't clear in the question I was asking. I was trying to dereference the double pointer in `set_year` so that I could set the year of the car. thanks. – redengineer Aug 29 '20 at 05:00

0 Answers0