C语言指针错误解析与代码修正
There are a few errors in this code:
- The line 'a=1:0=2;' should be 'a=1; b=2;' to initialize the values of 'a' and 'b'.
- The line 'p2=8b.' should be 'p2=&b;' to initialize the pointer 'p2' to the address of 'b'.
- The line 'p=p1p1=p2;p2=p;)' should be 'p=p1;p1=p2;p2=p;' to swap the pointers 'p1' and 'p2'.
- The line 'printf(*%d, %d\n',a,b);' should be 'printf('%d, %d\n',*p1,*p2);' to print the values pointed to by 'p1' and 'p2'.
Here is the corrected code:
#include <stdio.h>
int main() { int a, b, *p1, *p2, *p; a = 1; b = 2; p1 = &a; p2 = &b; if (a < b) { p = p1; p1 = p2; p2 = p; } printf('%d, %d\n', *p1, *p2); return 0; }
原文地址: https://www.cveoy.top/t/topic/ol5w 著作权归作者所有。请勿转载和采集!