【问题标题】:Read string from index 1 in C [closed]从C中的索引1读取字符串[关闭]
【发布时间】:2018-05-21 06:43:24
【问题描述】:

我正在尝试从索引 1 开始按字符数组使用字符串。

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

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

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

【问题讨论】:

  • but it didn't work。您遇到什么错误?
  • @DeiDei 我无法读取a+1的字符串
  • 怎么没用?

标签: c arrays string scanf


【解决方案1】:

但是没有用。有什么问题?扫描没有问题,但你可能会打印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;
}

【讨论】:

  • 我明白了,我没用a+1,我用了a。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多