gpt4 book ai didi

c - 为什么我的编译器不想编译这段代码?

转载 作者:行者123 更新时间:2023-11-30 14:38:46 24 4
gpt4 key购买 nike

昨天我遇到了这个有问题的代码,我一直想理解并纠正它。到目前为止我已经做了一些研究并纠正了它,但我想知道是否还有其他方法来纠正代码?

# include < stdio .h >
# include < stdlib .h >
int * sub ( int * x , int * y) { //return type should be pointer but is address//
int result = y - x ; //integer becomes pointer//
return &result;
}
int main ( void ) {
int x = 1;
int y = 5;
int * result = sub (&x , &y ); //function has addresses as parameters but not pointers//
printf ("% d\n" , * result );
return EXIT_SUCCESS ;
}

我会简单地删除所有指针和地址:

# include < stdio .h >
# include < stdlib .h >
int sub ( int x , int y) {
int result = y - x ;
return result ;
}
int main ( void ) {
int x = 1;
int y = 5;
int result = sub (x , y );
printf ("% d\n" , result );
return EXIT_SUCCESS ;
}

最佳答案

只需删除导入语句及其周围的空格:

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

int sub(int x, int y)
{
int result = y - x;
return result;
}

int main(void)
{
int x = 1;
int y = 5;
int result = sub(x, y);
printf("% d\n", result);
return EXIT_SUCCESS;
}

关于c - 为什么我的编译器不想编译这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com