我想使用指针数组、malloc 而不是使用字符数组来反转字符串(用户在运行时给出)。有人可以帮我给我一个代码吗?我对 C 很陌生。提前致谢。
最佳答案
我不确定你在问什么,但以下程序符合你的条件, 1.有一个指针数组 2.有malloc 3.没有字符数组
#include <stdio.h>
#include <stdlib.h>
#define STR_MAX_SIZE 256
int main()
{
char *str;
char *pos[2];
char c;
if((str = malloc(STR_MAX_SIZE)) ==NULL) {
return -1;
}
scanf("%s",str);
pos[0] = str;
pos[1] = str;
while(*pos[1]) {
pos[1]++;
}
pos[1] -= 1;
while(pos[0] < pos[1]) {
c = *pos[0];
*pos[0] = *pos[1];
*pos[1] = c;
pos[0]++;
pos[1]--;
}
printf("reversed : %s\n",str);
return 0;
}
关于c - 如何使用指针数组反转字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58179758/