【问题标题】:What does the message "[Error] invalid type argument of unary '*' (have 'int')" mean?消息“[Error] invalid type argument of unary '*' (have 'int')”是什么意思?
【发布时间】:2021-09-07 22:14:06
【问题描述】:
#include <stdio.h>

int main() {
        
    int i,j=1;
    int *jp1,jp2 = &j;
    
    jp1 = jp2;
    
    i = ++(*jp2);

//  *jp2 = *jp1 +i; // [Error] invalid type argument of unary '*' (have 'int')
    
    printf("i = %d,j = %d , *jp1 = %d , *jp2 = %d\n",i,j,*jp1,*jp2);    
    
    return 0;
}

【问题讨论】:

  • 您已将 jp2 声明为 int,而不是指向 int 的指针。试试int *jp1, *jp2 = &amp;j;
  • * 是变量,而不是类型。
  • Aaa 这就是为什么将多个变量声明塞进一行是一个坏主意。教你这样做的人需要改进。

标签: c pointers declaration dereference


【解决方案1】:

此声明

int *jp1,jp2 = &j;

等价于

int *jp1;
int jp2 = &j;

如您所见,您正试图将指针 &amp;j 分配给 jp2 类型的变量 int

您也不能将取消引用运算符* 应用于没有类似指针类型的对象

*jp2 = *jp1 +i;

int *jp1, *jp2 = &j;

虽然为了避免这样的错误,最好单独编写每个声明

int *jp1; 
int *jp2 = &j;

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2018-12-02
    相关资源
    最近更新 更多