There are a few syntax errors in this code, specifically with the use of Chinese characters instead of English characters for certain symbols. 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', a, b); return 0; }

This code declares four integer variables ('a', 'b', '*p1', '*p2') and a pointer variable '*p'. It assigns 'a' to 1 and 'b' to 2, and then assigns the memory addresses of 'a' and 'b' to 'p1' and 'p2', respectively.

The 'if' statement checks if 'a' is less than 'b'. If it is, the pointer variables 'p1' and 'p2' are swapped using a temporary variable 'p'.

Finally, the 'printf' statement prints the values of 'a' and 'b', separated by a comma and a space. The program then returns 0 to indicate successful completion.

C Programming: Swapping Variables Using Pointers

原文地址: https://www.cveoy.top/t/topic/ol5P 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录