C 使用指针更改字符数组的字符

C 使用指针更改字符数组的字符,c,arrays,pointers,character,C,Arrays,Pointers,Character,没有那么复杂,我的问题是我不知道如何使用指针更改字符数组的变量 #include "stdio.h" int main(void) { // Disable stdout buffering setvbuf(stdout, NULL, _IONBF, 0); char a[100], ch, *counter; int c = 0, i; counter = a[0]; printf("please enter a sentance:"); while ((ch = getchar()

没有那么复杂,我的问题是我不知道如何使用指针更改字符数组的变量

#include "stdio.h"

int main(void) {
// Disable stdout buffering
setvbuf(stdout, NULL, _IONBF, 0);

char a[100], ch, *counter;
int c = 0, i;

counter = a[0];
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter = ch;     //problem is here
    counter = a[c];
    c = c + 1;
}
printf("hi\n");

for(i = c-1; i >= 0; i--){
    printf("%c", a[i]);
}

return 0;
}
错误为“退出时状态为非零”

您需要以下信息

counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
    ++c;
}

while ( c != 0 ) printf("%c", a[--c]);
counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
}

while ( counter != a ) printf( "%c", *--counter );
或者甚至以下

counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
    ++c;
}

while ( c != 0 ) printf("%c", a[--c]);
counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
}

while ( counter != a ) printf( "%c", *--counter );
您需要以下内容

counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
    ++c;
}

while ( c != 0 ) printf("%c", a[--c]);
counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
}

while ( counter != a ) printf( "%c", *--counter );
或者甚至以下

counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
    ++c;
}

while ( c != 0 ) printf("%c", a[--c]);
counter = a;
^^^^^^^^^^^
printf("please enter a sentance:");


while ((ch = getchar()) != '\n'){
    printf("yo");
    *counter++ = ch;     //problem is here
}

while ( counter != a ) printf( "%c", *--counter );

有三个问题

  • 计数器=a[0]; 您正在将[0]处的值赋给指针。你需要的是这个
  • 计数器=&(a[0])

    或者更好

    计数器=a

  • 计数器=a[c] 与第1点相同。您不必这样做,因为计数器已经指向数组。只需增加指针

  • 由于数组的长度为100,所以只能存储99个字符+一个空字符。所以你需要一个柜台。所以用c作为计数器
  • 改变

    while ((ch = getchar()) != '\n'){
        printf("yo");
        *counter = ch;     //problem is here
        counter = a[c];
        c = c + 1;
    }
    


    有三个问题

  • 计数器=a[0]; 您正在将[0]处的值赋给指针。你需要的是这个
  • 计数器=&(a[0])

    或者更好

    计数器=a

  • 计数器=a[c] 与第1点相同。您不必这样做,因为计数器已经指向数组。只需增加指针

  • 由于数组的长度为100,所以只能存储99个字符+一个空字符。所以你需要一个柜台。所以用c作为计数器
  • 改变

    while ((ch = getchar()) != '\n'){
        printf("yo");
        *counter = ch;     //problem is here
        counter = a[c];
        c = c + 1;
    }
    


    计数器是否=a[0]编译时抛出错误?
    计数器=a[0]-->
    计数器=&a[0]或<代码>计数器=a
    计数器=a[c]-->
    计数器++行<代码>计数器=a[0]编译时抛出错误?
    计数器=a[0]-->
    计数器=&a[0]或<代码>计数器=a
    计数器=a[c]-->
    计数器++