简体   繁体   English

为什么我不能正确使用'->'? 我知道这可能是一个简单的概念性错误,但不知道为什么

[英]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. 我似乎无法发现为什么我在C语言中使用->命令是错误的。 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. 我知道'x-> y'等效于'(** x).y',但是由于某些原因,我在上课的第一个项目中错误地使用了它。

For perspective, I have 28 errors in my project; 从角度来看,我的项目中有28个错误; 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. 26是来自这种误解,两个是来自带有C99模式之外的初始声明的for循环-这对我来说并不重要,因为我必须将此代码转移到Unix机器上以在此处进行编译,因此这些错误不会发生。不会发生。

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'). 在这26个错误中,每个错误的结构都是这样,但有人说(有“结构头”),而有人说(有“结构标签”)。

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){} ^这是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? 有人可以解释我在C语言中对该解引用器的理解不正确的地方,为什么这些调用按我期望的那样工作? I'm quite confused, and my google/searching skills have turned up nothing in regards to the event. 我很困惑,我的Google /搜索技能对这次活动没有任何帮助。 Thank you for your help. 谢谢您的帮助。

Here is my pastebin: http://pastebin.com/ck8ThFLG 这是我的pastebin: http//pastebin.com/ck8ThFLG

a->b is equivalent to (*a).b a->b等效于(*a).b

Since intro is of type struct header , its members are accessible simply with a . 由于intro的类型为struct header ,因此可以使用来轻松访问其成员. while the -> would be required for (say) an intro_ptr variable of type struct header* . (例如) struct header*类型的intro_ptr变量需要->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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