由于指针,乘法结果始终为零?

the multiplication result is always zero because of pointers?

我正在尝试编写一个程序,从用户那里获取两个整数并将它们分配给指针,然后将这些指针传递给函数以将它们相乘并打印结果。但是输出始终为 0。我是初学者,我不明白自己做错了什么。感谢帮助。这是我的代码:

#include <stdio.h>

int x, y, m;

void point(int *xPtr, int *yPtr);

int main()
{
    int *xPtr, *yPtr;
    
    printf("Enter two integers: \n");
    scanf("%d %d", &x, &y);

    point(&x, &y);

    m = (*xPtr)*(*yPtr);

    xPtr = &m;

    printf("The result is %d", *xPtr);

    return 0;
}

void point(int *xPtr, int *yPtr)
{
    xPtr = &x;
    yPtr = &y;
}

main 中变量 xPtryPtr 从未被初始化

point 中的赋值是局部的,不会影响 point,如果你想修改具有相同名称的变量main 你可以这样做:

void point(int **xPtr, int **yPtr)
{
    *xPtr = &x;
    *yPtr = &y;
}

当然还要更改point的声明,并将main中的调用更改为:

point(&xPtr, &yPtr);

... However the output is always 0

我不认为你给出的程序有任何输出,因为你很可能无法打印,你尊重未初始化的指针,行为是未定义的,通常是灾难性的,如分段错误


修改您的程序:

#include <stdio.h>

int x, y, m;

void point(int **xPtr, int **yPtr);

int main()
{
    int *xPtr, *yPtr;
    
    printf("Enter two integers: \n");
    scanf("%d %d", &x, &y);

    point(&xPtr, &yPtr);

    m = (*xPtr)*(*yPtr);

    xPtr = &m;

    printf("The result is %d", *xPtr);

    return 0;
}

void point(int **xPtr, int **yPtr)
{
    *xPtr = &x;
    *yPtr = &y;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
Enter two integers: 
2 3
The result is 6pi@raspberrypi:/tmp $ 
pi@raspberrypi:/tmp $ 
pi@raspberrypi:/tmp $ valgrind ./a.out
==5992== Memcheck, a memory error detector
==5992== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5992== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5992== Command: ./a.out
==5992== 
Enter two integers: 
2 3
The result is 6==5992== 
==5992== HEAP SUMMARY:
==5992==     in use at exit: 0 bytes in 0 blocks
==5992==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==5992== 
==5992== All heap blocks were freed -- no leaks are possible
==5992== 
==5992== For lists of detected and suppressed errors, rerun with: -s
==5992== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $ 

出那个更好:

  • 检查 scanf returns 2 了解输入是否有效,
  • 打印最终结果 \n 因为在终端中执行程序时无法读取结果,
  • 以高警告级别编译并考虑生成警告

关于最后的评论和使用您的代码版本:

 pi@raspberrypi:/tmp $ gcc -g -Wall cc.c
 cc.c: In function ‘main’:
 cc.c:16:10: warning: ‘xPtr’ is used uninitialized in this function [-Wuninitialized]
      m = (*xPtr)*(*yPtr);
          ~^~~~~~
 cc.c:16:18: warning: ‘yPtr’ is used uninitialized in this function [-Wuninitialized]
      m = (*xPtr)*(*yPtr);
                 ~^~~~~~

如果无论如何我在 valgrind 下执行它:

pi@raspberrypi:/tmp $ valgrind ./a.out
==7143== Memcheck, a memory error detector
==7143== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7143== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==7143== Command: ./a.out
==7143== 
1 2Enter two integers: 

==7143== Use of uninitialised value of size 4
==7143==    at 0x104BC: main (cc.c:16)
==7143== 
==7143== Invalid read of size 4
==7143==    at 0x104BC: main (cc.c:16)
==7143==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7143== 
==7143== 
==7143== Process terminating with default action of signal 11 (SIGSEGV)
==7143==  Access not within mapped region at address 0x0
==7143==    at 0x104BC: main (cc.c:16)
==7143==  If you believe this happened as a result of a stack
==7143==  overflow in your program's main thread (unlikely but
==7143==  possible), you can try to increase the size of the
==7143==  main thread stack using the --main-stacksize= flag.
==7143==  The main thread stack size used in this run was 8388608.
==7143== 
==7143== HEAP SUMMARY:
==7143==     in use at exit: 0 bytes in 0 blocks
==7143==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==7143== 
==7143== All heap blocks were freed -- no leaks are possible
==7143== 
==7143== Use --track-origins=yes to see where uninitialised values come from
==7143== For lists of detected and suppressed errors, rerun with: -s
==7143== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Erreur de segmentation
pi@raspberrypi:/tmp $ 

由于你是在修改指针本身,所以你需要将指针传递给这些指针(并在调用函数时传递指针的地址):

point(&xPtr, &yPtr);

像这样定义你的函数:

void point(int **xPtr, int **yPtr) {
    *xPtr = &x;
    *yPtr = &y;
}

别忘了更改声明

void point(int **xPtr, int **yPtr);