简体   繁体   English

'一元*'的无效类型参数(具有'int')。 基本上我在指针和结构的逻辑上遇到麻烦

[英]invalid type argument of 'unary *' (have 'int'). Basically I'm having trouble with the logic of pointers & structs together

I have this code here and it's giving me compiler errors and I understand that there are dozens of other questions on here with a title basically the exact same; 我在这里有这段代码,它给了我编译器错误,而且我知道这里还有很多其他问题,标题基本上完全相同。 I've read through quite a few of them and haven't had all my questions answered. 我已经阅读了其中许多内容,但并未回答我所有的问题。 I've read a lot about pointers and structures in the past few days on stackoverflow as well as teaching sites, again dozens of them and I feel I just need a physical example in front of me to dissected and explained. 在过去的几天里,我在stackoverflow以及教学站点上已经阅读了很多有关指针和结构的信息,其中又有数十篇,我觉得我只需要一个物理示例来剖析和解释。 I feel like I know most of this stuff but I quite more unsure of myself quite more than I'd like to be. 我觉得我知道大多数这些东西,但是我对自己的不确定性比我想做的要多得多。 If someone could help me out to understand what Is going on. 如果有人可以帮助我了解正在发生的事情。 I've posted a numbered list below the tell you what I think is happening and lets hope I'm right for the most part, less explaining for you to have to do haha just kidding. 我在下面发布了一个编号列表,告诉您我的想法,并希望我在大多数情况下是对的,而不必为您做些开玩笑的哈哈解释。 Well here is the code. 好了,这里是代码。

struct foo{
    int num;
    char  *word;
    struct foo *ptr;
};

void func1(struct foo*);
void func2(struct foo);

int main() {

    struct foo a;
    a.num = 20;
    a.word = "wordOne";
    func1(&a);
    printf("%d %s", a.num, a.word);       // refer to #3 below.

    a.ptr = &a;
    a.num = 55;
    a.word = "thirdToLastWord";
    func2(a);
    printf("%d %s\n", a.num, a.word);
}

void func1(struct foo *a)
{
    while(*(a->word) != '\0')
        {
            putchar(*(a->word));
            a->word++;
        }
    putchar('\n');
    if(a->num % 10 != 0)
        { a->num *= 2; }
    a->word--;
    printf("num equals %d\n", (*a).num);
}

void func2(struct foo a)
{
    if((*a).num == a->ptr->num)               //compiler error
        {(*a).num = (*((*a).ptr)).num +1; }   //compiler error
    else    {   a->num = 200;   }
    a->word = "wordsix";
    a->ptr->word = "wordseven";
    printf("function 2 %d %s\n", (*a).num, (*((*a).ptr)).word);}
}

Ok. 好。 So I think I understand the first function. 所以我想我了解第一个功能。 But please correct me on any of the below if I am wrong or it seems like I have a faulty understanding of why its doing what it does. 但是,如果我错了,或者我对它为什么要做的事情有一个错误的理解,请对以下任何一项进行纠正。

1)Before main func1 declares the struct "foo" as a pointer. 1)在主func1声明结构“ foo”作为指针之前。 When func1 is called we pass in the address of "a" so we know what address to point to. 当调用func1时,我们传入“ a”的地址,因此我们知道要指向的地址。

2)Everything within func1 will change the original struct. 2)func1中的所有内容都会更改原始结构。 The word is decremented down to only show the last letter "e" when printed out outside of func1. 当在func1外部打印时,该词递减以仅显示最后一个字母“ e”。

3) on the line I commented #3 that is printing out the original struct foo's .num not the pointers version (even though they would be the same thing). 3)在一行中,我评论了#3,它打印出了原始struct foo的.num而不是指针版本(即使它们是同一个东西)。

I hope I haven't messed up so far. 我希望到目前为止还没有弄乱。

4) Ok so the a.ptr = &a Sets the struct var "struct foo *ptr" with in the original foo structure equal to the address of a; 4)好了,所以a.ptr =&a将原始foo结构中的struct var“ struct foo * ptr”设置为等于a的地址; which happens to be the exact struct that the pointer "struct foo *ptr" finds himself in. Right? 恰好是指针“ struct foo * ptr”找到自己的确切结构。对吗?

5) And we are referring to him as a member of a (the main/orig structure) he has already been established as a pointer above main. 5)我们指的是他是(main / orig结构)的成员,他已经被确立为main之上的指针。

6) So does this make an infinite loop of structures within structures that are linked together by a pointer within themselves? 6)那么这是否使结构内部的结构无限循环,并通过自身内部的指针将它们链接在一起?

Where I commented compiler error is where I'm positive I'm having problems. 我认为编译器错误是我肯定会遇到问题的地方。

7) First off, after setting a.ptr = &a; 7)首先,设置a.ptr =&a; we pass a copy of struct, with the initialized pointer "ptr", into the function func2, Yes? 我们将带有初始化指针“ ptr”的struct副本传递给函数func2,是吗?

8) In the first if stmt it looks like we are comparing a->word to a->ptr->num. 8)在第一个if stmt中,看起来我们正在比较a-> word与a-> ptr-> num。 Which I assume is meaning to say: IF { the member "word" belonging to the struct that "a" is pointing to is equal to member "num", belonging to the struct that pointer "ptr" is pointing to and "ptr is again being pointed to by a pointer "a" } That seems right but I don't think it is.... 我假设的意思是说:IF {属于成员“ a”指向的结构的“单词”等于成员“ num”,属于指针“ ptr”指向的结构,而“ ptr是再次由指针“ a”指向}看起来不错,但我认为不是。

9) If that is true then the line below say the exact same thing with a +1 added on. 9)如果是这样,则下面的行完全相同,并加上+1。

10) And I suppose finally the final printf within main prints out either what num and word were before they went into func2 or some pointer(s) within func2 have changed their values. 10)最后,我想主函数中的最终printf会打印出进入func2之前的num和word或func2中的某些指针更改了它们的值。

I would greatly appreciate all your feedback and suggestions of what I should do to ingrain this pointer && structure ptr->member and/or structure.member stuff in my head. 我将非常感谢您提出的所有反馈和建议,这些建议和建议使我牢记此指针&&结构ptr-> member和/或structure.member。

Also, I'm not sure if this came up in my problem but I am fairly shaky with double pointers as well. 另外,我不确定这是否出现在我的问题中,但我对双指针也相当不满意。 If its not too much trouble could someone show me how a double pointer could have integrated itself into my code here? 如果不是很麻烦,有人可以告诉我如何将双指针本身集成到我的代码中吗?

Thank you 谢谢

a is not a pointer. a不是指针。 You are using -> operator to access non-pointer struct members on many occasions which is giving you the errors. 您经常使用->运算符来访问非指针struct成员,这给了您错误。

There are many errors in your code. 您的代码中有很多错误。 For example, you are not allocating memory for *word . 例如,您没有为*word分配内存。 But storing a value in it: 但是在其中存储一个值:

a.word = "wordOne";

You should access struct member num as a.num . 您应该将struct成员num作为a.num访问。

Here's a tutorial on using pointers to structures which might help you understand it. 这是有关使用指向结构的指针的教程,这可能有助于您理解它。

There really are quite a few errors in this code, I'll attempt to go down through your list for clarity's sake. 这段代码中确实有很多错误,为清楚起见,我将尝试遍历您的列表。

1.) Yes, you can pass a pointer here, but it is necessary to check for NULL before dereferencing a pointer parameter. 1.)是的,您可以在此处传递指针,但是在取消引用指针参数之前必须检查NULL。 If you get into the habit of doing this now, you'll save yourself countless hours tracking down the source of segmentation faults. 如果您现在就习惯这样做,那么您将节省数不清的时间来跟踪细分错误的来源。

2.) func1 doesn't change a->num 2.) func1不会更改a->num

if(a->num % 10 != 0)
    { a->num *= 2; }
// a->num % 10 = 20 % 10 = 0, and 0 != 0 is false

Also, Blue Moon is correct about a->word , this could be done correctly one of two ways. 另外,Blue Moon关于a->word是正确的,这可以通过以下两种方法之一正确完成。

With a global: 与全球:

const char *global = "wordOne";

int main(int argc, char *argv[]) {
  a.work = (char*)global;
  /*...*/
}

Or the preferred way that Blue Moon is referring to: 或“蓝色月亮”所指的首选方式:

const char *global = "wordOne";

int main(int argc, char *argv[]) {
  size_t sz = sizeof(global);
  a.work = (char*)malloc(sz);
  for(int i = 0; i < sz; i++) { a.work[i] = global[i]; }
  /*...*/
}

Note: With the second method you will eventually need to free() that memory. 注意:使用第二种方法,您最终将需要free()该内存。

Read a bit more on pointers and you'll understand why 阅读更多有关指针的内容,您会明白为什么

In regards to func2() , this function doesn't use a pointer argument, so dereferencing it is sure to throw a compilation error. 关于func2() ,此函数不使用指针参数,因此取消引用它肯定会引发编译错误。

void func2(struct foo);
// struct foo is passed 'by value'

I really hope that helps clear things up! 我真的希望这有助于清理问题! :-) :-)

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

相关问题 出现错误:一元“*”的类型参数无效(有“int”) - Having error:invalid type argument of unary '*" (have 'int') 错误:一元&#39;*&#39;的类型参数无效(具有&#39;int&#39;) - error : invalid type argument of unary '*' (have 'int') 一元&#39;*&#39;的类型参数无效(具有int吗? - Invalid type argument of unary '*' (have int? 错误:&#39;unary *&#39;的无效类型参数(有&#39;int&#39;)| - error: invalid type argument of 'unary *' (have 'int')| 错误:一元'*'的无效类型参数(有'int') - error: invalid type argument of unary '*' (have 'int') &#39;一元*&#39;的无效类型参数(具有&#39;int&#39;) - invalid type argument of 'unary *' (have 'int') 错误:一元&#39;*&#39;的无效类型参数(有&#39;int&#39;) - error: invalid type argument of unary '*' (have 'int') 一元&#39;*&#39;的无效类型参数(具有&#39;int&#39;) - invalid type argument of unary '*' (have 'int') 错误:&#39;unary *&#39;的无效类型参数(有&#39;int&#39;) - error: invalid type argument of ‘unary *’ (have ‘int’) 错误:一元'*'的无效类型参数(有'int')*(*(m + r)+ c)= s; //将其保存在结果矩阵中 - error: invalid type argument of unary ‘*’ (have ‘int’) *(*(m+r)+c) = s; //save it in the resulting matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM