C - 字符串给我错误

Ves*_*ske 1 c gcc

所以我正在编写一个简单的代码来打印出我的字符串中的每个符号.编译时,它给我一个难以理解的错误,我不明白:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
    char my_string[50];
    int i, n;
    printf("Type in a string please : ");
    scanf("%s", &my_string);
    n = strlen(my_string);
    for (i = 0;i < n; i++) {
        printf("%c",my_string[i]);
    }

}
Run Code Online (Sandbox Code Playgroud)

它给出的错误:

gcc yl2.c -o Yl2
yl2.c: In function ‘main’:
yl2.c:9:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]
  scanf("%s", &my_string);
  ^
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

Sun*_*lly 6

scanf("%s", &my_string);不应该有&.

因为你已经将char my_string[50];my_string 声明为字符数组,它是一种char *期望scanf()作为警告状态的类型.

只是用,scanf("%s", my_string);.作为参数的数组的基址是足够的.