C 中指针与整数相加

发布于 2024-10-20 21:43:40 字数 740 浏览 5 评论 0原文

假设我有 int *a、int *b、int *c 并假设 ab 已经指向一些整数。

我想将整数添加到 ab 中,并将它们保存到 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 技术交流群。

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

发布评论

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

评论(2

七度光 2024-10-27 21:43:40

这是一个工作示例:

#include <stdio.h>

int main( int argc, const char* argv[] )
{
    int x = 1;
    int y = 2;
    int z = 0;
    int *a = &x;
    int *b = &y;
    int *c = &z;

    *c = *a + *b;

    printf( "%d + %d = %d\n", *a, *b, *c );
    return 1;
}

运行收益:

./a.out 
1 + 2 = 3

您可能遇到的常见错误:

  1. 未将 a、b 或 c 指向有效
    记忆。这将导致您的程序崩溃。
  2. 打印的值
    指针 (a) 而不是它的值
    指向 (*a)。这将导致显示非常大的数字。
  3. 不解引用赋值 c = *a + *b 而不是 *c = *a + *b。在这种情况下,当您尝试在赋值后取消引用 c 时,程序将会崩溃。

Here is a working example:

#include <stdio.h>

int main( int argc, const char* argv[] )
{
    int x = 1;
    int y = 2;
    int z = 0;
    int *a = &x;
    int *b = &y;
    int *c = &z;

    *c = *a + *b;

    printf( "%d + %d = %d\n", *a, *b, *c );
    return 1;
}

Running yields:

./a.out 
1 + 2 = 3

Common errors you might have encountered:

  1. Not pointing a, b or c at valid
    memory. This will result in your program crashing.
  2. Printing the value of the
    pointer (a) rather than the value it
    points to (*a). This will result in a very large number being displayed.
  3. Not dereferencing the assignment c = *a + *b rather than *c = *a + *b. In this case, the program will crash when you try to dereference c after the assignment.
三五鸿雁 2024-10-27 21:43:40

如果 Ovendx、Ovendy 指向有效的内存位置,则要将值分配给该位置,您需要取消引用它们。所以,应该是 -

(*OVendx) = (*OVx) + (*OVw);
(*OVendy) = (*OVy) + (*OVh);

您没有在发布的代码片段中取消引用。

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 -

(*OVendx) = (*OVx) + (*OVw);
(*OVendy) = (*OVy) + (*OVh);

You aren't dereferencing in the snippet posted.

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