【问题标题】:unknown error arriving after the compiling and running of this c program此c程序编译运行后出现未知错误
【发布时间】:2017-11-27 20:57:07
【问题描述】:

我已经编写了这段代码,并尝试使用默认的 GCC 编译器在代码块中编译它。
当给出选项 1 时,我得到这个输出“hello world6356744”。 请解释发生了什么。

#include<stdio.h>
#include<stdlib.h>

int fun1(int x,int y)
{
printf("hello world");
return x+y;
}
int fun2()
{
return 0;
}


int main()
{
printf("type 1 for * or type 2 for #");
int a;
scanf("%d", &a);

int ca1;

switch(a)
{
case 1:

    ca1=fun1(2,3);
    printf("%d", &ca1);
    break;

case 2:
    fun2();
    break;
default:
    printf("invalid input");

}
return 0;
}

https://i.stack.imgur.com/A5PhT.png

【问题讨论】:

  • 你希望它做什么?该输出是我所期望的

标签: c function switch-statement


【解决方案1】:

您在第 28 行使用 & 打印指针的地址。删除 & 和 printf 应该可以正常工作。 Printf 不采用地址,而是采用值。

使用scanf读取输入时,即提供变量/指针的地址时,因为scanf直接修改了您传递给它的变量/指针。

【讨论】:

  • 没问题,即使是经验丰富的 c 和 c++ 开发人员也会遇到这个问题。书中有一整章专门讨论“通过引用传递变量”和“通过值传递变量”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2020-10-30
  • 2015-09-11
  • 2013-12-21
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
相关资源
最近更新 更多