C 中指针与整数相加
假设我有 int *a、int *b、int *c
并假设 a
和 b
已经指向一些整数。
我想将整数添加到 a
和 b
中,并将它们保存到 c
指向
This:
*c = *a + *b;
不起作用的任何位置。它总是吐出“‘一元 *’的参数无效。为什么会这样?
附加信息: 这是我尝试实现它的方法:
int getCoordinates(int argc, char *argv[], FILE *overlay, FILE *base, int *OVx, int *OVy, int *OVendx, int *OVendy, int *Bx, int *By, int *Bendx, int *Bendy)
{
... // OVx and OVw are assigned here. I know it works so I won't waste your time with this part.
// Set overlay image's x and y defaults (0,0).
*OVx = 0;
*OVy = 0;
...
OVendx = (*OVx) + (*OVw);
OVendy = (*OVy) + (*OVh);
Say i have int *a, int *b, int *c
and say a
and b
already point to some integers.
I want to add the integers down a
and b
and save them to wherever c
is pointing to
This:
*c = *a + *b;
does not work. It always spits out "invalid argument of 'unary *'. Why so?
ADDITIONAL INFO:
here's how I'm trying to implement it:
int getCoordinates(int argc, char *argv[], FILE *overlay, FILE *base, int *OVx, int *OVy, int *OVendx, int *OVendy, int *Bx, int *By, int *Bendx, int *Bendy)
{
... // OVx and OVw are assigned here. I know it works so I won't waste your time with this part.
// Set overlay image's x and y defaults (0,0).
*OVx = 0;
*OVy = 0;
...
OVendx = (*OVx) + (*OVw);
OVendy = (*OVy) + (*OVh);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个工作示例:
运行收益:
您可能遇到的常见错误:
记忆。这将导致您的程序崩溃。
指针 (a) 而不是它的值
指向 (*a)。这将导致显示非常大的数字。
Here is a working example:
Running yields:
Common errors you might have encountered:
memory. This will result in your program crashing.
pointer (a) rather than the value it
points to (*a). This will result in a very large number being displayed.
如果 Ovendx、Ovendy 指向有效的内存位置,则要将值分配给该位置,您需要取消引用它们。所以,应该是 -
您没有在发布的代码片段中取消引用。
If Ovendx, Ovendy are pointing to a valid memory locations, then to assign values to that location, you need to dereference them. So, it should be -
You aren't dereferencing in the snippet posted.