简体   繁体   中英

Why am I not using '->' correctly? I know it is probably a simple conceptual mistake, but don't know why

I'm sorry, I'm slightly embarrassed that I have to ask this question. I can't seem to discover why I'm using the -> command in C wrong. I know that 'x->y' is equivalent to '(*x).y', but for some reason I am using it incorrectly in my first project for class.

For perspective, I have 28 errors in my project; 26 are from this misunderstanding, and two are from a for loop with initial declaration outside of C99 mode - this doesn't matter to me as I have to transfer this code over to a Unix machine to compile it there, so these errors won't occur.

The specific error I'm getting is this:

C:\Users\Nick\Documents\School\CS449\Programs\Project 1\EXIF Viewer\main.c|78|error:invalid type argument of '->' (have 'struct tag')|

Of the 26 errors, every single one is structured like this, but some say (have 'struct header') while others say (have 'struct tag').

I'm going to include the entirety of my code at the bottom of this post, but the relevant struct declarations and calls are set like this:

struct tag the_tag;//The reused tag

^This is a global struct variable

struct header intro; //Bytes 0-19 of the program

^This is a local struct header in int main(argc, **argv){}

Some of the lines that are giving me these errors are:

56:  else if(intro->endianness[0] != 'I')
74:  if(the_tag->tag_identifier == (0x010F))
78:  printf("Manufacturer: %s\n", the_tag->data);
98:  else if(the_tag->tag_identifier == (0x08769))
125: else if(tagger->tag_identifier == 0x8827)

Can someone please explain where my understanding of this dereferencer in C is incorrect, and why these calls are working like I expect them to? I'm quite confused, and my google/searching skills have turned up nothing in regards to the event. Thank you for your help.

Here is my pastebin: http://pastebin.com/ck8ThFLG

a->b is equivalent to (*a).b

Since intro is of type struct header , its members are accessible simply with a . while the -> would be required for (say) an intro_ptr variable of type struct header* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM