C指针-Segment fault的解决方法是什么
C pointers-What is the solution of Segment fault
为什么会输出“Segmentation fault(core dumped)
当我运行下面的代码时,它说段错误(核心转储)
#include <stdio.h>
void swap(int *, int *);
int main() {
int x = 5, y = 10;
swap(x, y);
printf("%i\n %i\n", x, y);
}
void swap(int *a, int *b) {
int s;
s = *a;
*a = *b;
*b = s;
}
问题在行swap(x, y);
您传递的是值而不是地址。你应该有编译器警告。
使用swap(&x, &y);
为什么会输出“Segmentation fault(core dumped)
当我运行下面的代码时,它说段错误(核心转储)
#include <stdio.h>
void swap(int *, int *);
int main() {
int x = 5, y = 10;
swap(x, y);
printf("%i\n %i\n", x, y);
}
void swap(int *a, int *b) {
int s;
s = *a;
*a = *b;
*b = s;
}
问题在行swap(x, y);
您传递的是值而不是地址。你应该有编译器警告。
使用swap(&x, &y);