二进制 / 的操作数无效(具有“int *”和“int”)?

发布于 2024-12-20 11:57:26 字数 994 浏览 1 评论 0原文

每次我尝试这个:(

long crypt(int *integer)
{
    printf("Enter five digit integer:\n");  
    scanf("%i",integer);
    
    int digit1=integer/10000;
    int digit2=(integer%10000)/1000;
    int digit3=(integer%1000)/100;
    int digit4=(integer%100)/10;
    int digit5=(integer%10)/1;

    const char *digit1c[10];
    const char *digit2c[10];
    const char *digit3c[10];
    const char *digit4c[10];
    const char *digit5c[10];

    /...
}

还有更多,但这似乎是问题,我将根据请求添加其余部分。)

然后它返回此错误:

math2.h:44:20: error: invalid operands to binary / (have ‘int *’ and ‘int’)
math2.h:45:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:46:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:47:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:48:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)

我知道它与我用来初始化数字和的运算符有关我确实尝试将它们的类型更改为 int * ,但这不起作用。 那么这里究竟发生了什么?

Every time I try this:

long crypt(int *integer)
{
    printf("Enter five digit integer:\n");  
    scanf("%i",integer);
    
    int digit1=integer/10000;
    int digit2=(integer%10000)/1000;
    int digit3=(integer%1000)/100;
    int digit4=(integer%100)/10;
    int digit5=(integer%10)/1;

    const char *digit1c[10];
    const char *digit2c[10];
    const char *digit3c[10];
    const char *digit4c[10];
    const char *digit5c[10];

    /...
}

(There's more but this seems to be the problem, I'll add the rest by request.)

then it return this error:

math2.h:44:20: error: invalid operands to binary / (have ‘int *’ and ‘int’)
math2.h:45:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:46:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:47:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)
math2.h:48:21: error: invalid operands to binary % (have ‘int *’ and ‘int’)

I know it has something to do with the operators I used to initialize the digits and I did try changing their type to int * but that didn't work.
So what's happening here exactly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠栀 2024-12-27 11:57:26

integer 是一个指向 int (int*) 的指针,所以当你想使用 int 时,它指向,您需要取消引用它:

int digit1=(*integer)/10000; // and so on...

integer is a pointer to int (int*), so when you want to use the int it points to, you need to dereference it:

int digit1=(*integer)/10000; // and so on...
肩上的翅膀 2024-12-27 11:57:26

参数integer不是一个int对象;它是一个int*对象,即一个指针。 (integer 是指针对象的一个​​误导性名称。)

如果您将:更改

int digit1=integer/10000;

int digit1 = *integer / 10000;

并对其余代码进行相应的更改,它至少会编译。 integer 是一个指针; *integer 是它指向的 int 对象。

(此外,二元运算符周围的空格将使您的代码更易于阅读。使用空格的另一个好理由是,如果颠倒除法,则:

int digit1=10000/*integer;

将引入 /* ... */评论。)

The parameter integer is not an int object; it's an int* object, i.e., a pointer. (And integer is a misleading name for a pointer object.)

If you change:

int digit1=integer/10000;

to

int digit1 = *integer / 10000;

and make corresponding changes to the rest of your code, it will at least compile. integer is a pointer; *integer is the int object that it points to.

(Also, spaces around binary operators would make your code easier to read. Another good reason to use whitespace is that, if the division were reversed, then this:

int digit1=10000/*integer;

would introduce a /* ... */ comment.)

荒路情人 2024-12-27 11:57:26

这里的问题是,integer 的类型是 int *,这是一个不支持 /%< 运算符的指针/代码>。您的问题有两种解决方案。

  1. 取消引用整数,每次使用*运算符执行算术运算时都使用它。
    例如 int digital1=(*integer)/10000;

  2. integer 更改为 int 类型。您必须更改函数的签名,并且必须更改 scanf 以使用 & 运算符 scanf("%i",&integer);

The issue here is that the type of integer is int *, a pointer which does not support the operators of / and %. There are two solutions to your problem.

  1. Dereference integer, by using the * operator every time you perform arithmatic with it.
    e.g. int digit1=(*integer)/10000;

  2. Change integer to the type int. You have to change the signature of the function, and the scanf will have to be changed to use the & operator, scanf("%i",&integer);

辞旧 2024-12-27 11:57:26

我们可以通过取消引用来添加整数指针来执行操作
即通过添加*pointer

取消引用后我们实际做的是访问指针所指向的值。

int a = 10; int *ptr = &a;

ptr 将具有变量 a 的地址,而 *ptr 将具有存储在 a 中的值。

We can add the integer pointers by dereferencing it to perform operations
i.e. by adding * pointer

What we actually do after dereferencing is we access the value at which the pointer is pointing.

int a = 10; int *ptr = &a;

ptr will have the address of the variable a, while *ptr will have the value stored in a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文