二进制 / 的操作数无效(具有“int *”和“int”)?
每次我尝试这个:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
integer
是一个指向int
(int*
) 的指针,所以当你想使用 int 时,它指向,您需要取消引用它:integer
is a pointer toint
(int*
), so when you want to use the int it points to, you need to dereference it:参数
integer
不是一个int
对象;它是一个int*
对象,即一个指针。 (integer
是指针对象的一个误导性名称。)如果您将:更改
为
并对其余代码进行相应的更改,它至少会编译。
integer
是一个指针;*integer
是它指向的int
对象。(此外,二元运算符周围的空格将使您的代码更易于阅读。使用空格的另一个好理由是,如果颠倒除法,则:
将引入
/* ... */
评论。)The parameter
integer
is not anint
object; it's anint*
object, i.e., a pointer. (Andinteger
is a misleading name for a pointer object.)If you change:
to
and make corresponding changes to the rest of your code, it will at least compile.
integer
is a pointer;*integer
is theint
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:
would introduce a
/* ... */
comment.)这里的问题是,
integer
的类型是int *
,这是一个不支持/
和%< 运算符的指针/代码>。您的问题有两种解决方案。
取消引用
整数
,每次使用*
运算符执行算术运算时都使用它。例如
int digital1=(*integer)/10000;
将
integer
更改为int
类型。您必须更改函数的签名,并且必须更改 scanf 以使用&
运算符scanf("%i",&integer);
The issue here is that the type of
integer
isint *
, a pointer which does not support the operators of/
and%
. There are two solutions to your problem.Dereference
integer
, by using the*
operator every time you perform arithmatic with it.e.g.
int digit1=(*integer)/10000;
Change
integer
to the typeint
. You have to change the signature of the function, and the scanf will have to be changed to use the&
operator,scanf("%i",&integer);
我们可以通过取消引用来添加整数指针来执行操作
即通过添加
*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.