You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

指针导致乘法结果始终为0?C语言初学者求调试帮助

Fixing Your C Program's Zero Output Issue

Hey there! Let's dig into why your multiplication result is always 0. I'll walk through the problems in your code and show you how to fix it step by step.

First, let's reference your original code:

#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; 
}

Key Problems in Your Code

  • Uninitialized wild pointers in main: The xPtr and yPtr variables in main are declared but never assigned a valid memory address before you try to dereference them with (*xPtr)*(*yPtr). This is a critical issue—accessing uninitialized pointers leads to undefined behavior (you're getting 0 here, but it could just as easily crash your program or return garbage values).
  • Pointless point function: In C, function arguments are passed by value. That means when you pass &x and &y to point, the function gets copies of those pointers. When you do xPtr = &x; inside point, you're only modifying the local copy of the pointer, not the original xPtr in main. This function does nothing to help your program right now.

Fixed Code

Here's a corrected version that works as intended, with better practices included:

#include <stdio.h> 

// Let's make a function that actually handles the multiplication
void calculateProduct(int *num1, int *num2, int *result); 

int main() { 
    int x, y, product; 
    int *xPtr = &x, *yPtr = &y; // Initialize pointers to valid addresses immediately
    
    printf("Enter two integers: \n"); 
    scanf("%d %d", &x, &y); 
    
    // Pass pointers to our numbers and the result variable
    calculateProduct(xPtr, yPtr, &product); 
    
    printf("The result is %d", product); 
    return 0; 
} 

// This function uses pointers to modify the original result variable in main
void calculateProduct(int *num1, int *num2, int *result) {
    *result = (*num1) * (*num2);
}

What Changed?

  1. Removed global variables: Global variables are generally bad practice (they make code harder to debug and maintain), so we moved x, y, and the result variable inside main.
  2. Properly initialized pointers: xPtr and yPtr are set to point to x and y the moment they're declared, eliminating wild pointers.
  3. Useful function: The renamed calculateProduct function takes a pointer to the result variable. By dereferencing *result, we directly modify the product variable in main—no useless pointer copies here.
  4. Simplified logic: We can print the result directly without reassigning pointers, making the code easier to follow.

If you want to keep things even simpler (no helper function needed), you can compute the product directly in main once your pointers are valid:

#include <stdio.h> 

int main() { 
    int x, y, product; 
    int *xPtr = &x, *yPtr = &y; 
    
    printf("Enter two integers: \n"); 
    scanf("%d %d", &x, &y); 
    
    product = (*xPtr) * (*yPtr); 
    printf("The result is %d", product); 
    return 0; 
}

This works because xPtr and yPtr are properly pointing to x and y from the start.

内容的提问来源于stack exchange,提问作者gece

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:20:35