我如何在我的程序中使用指针来跟踪数组的当前位置,而不是使用“计数器”?
#include <stdio.h>
#include <string.h>
#define MAX 100
int main(void){
int counter = 0, c;
char *charPtr, characterArray[MAX] = { 0 };
printf("Enter a message: ");
for (c = getchar(); c != EOF && c != '\n'; c = getchar()){
charPtr = &characterArray[0];
characterArray[*charPtr++] = c;
}
counter = strlen(characterArray) - 1;
printf("The reverse order is: ");
while (counter >= 0){
printf("%c", characterArray[counter]);
--counter;
}
printf("\n\n");
return 0;
}
最佳答案
大致是这样的:
char *endptr = characterArray + strlen(characterArray) - 1;
printf("The reverse order is: ");
while (endptr >= characterArray){
printf("%c", *endptr--);
}
代码未经测试。
关于c - 使用指针导航数组,而不是使用 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554992/