首页 > 解决方案 > 从C中的索引1读取字符串

问题描述

我正在尝试使用索引 1 中的 char 数组的字符串。

char a[100];
scanf("%s", a+1);

我以为它会很好用,但它没有用。怎么了?

如何跳过索引 0 并从索引 1 读取字符串?

标签: carraysstringscanf

解决方案


但它没有用。怎么了? 扫描没有错,但您可能会打印a,首先初始化 char 数组。

int main(void) {
        char a[100] = { 0 }; /* initilize with 0 to avoid printing junk data */
        scanf("%s", a+1);
        printf("[%s]\n",a);/* nothing it prints bcz you putted data in a+1 */
        printf("[%s]\n",a+1); /* it prints what you scanned in a+1 */
        return 0;
}

推荐阅读