c - 从C中的索引1读取字符串
问题描述
我正在尝试使用索引 1 中的 char 数组的字符串。
char a[100];
scanf("%s", a+1);
我以为它会很好用,但它没有用。怎么了?
如何跳过索引 0 并从索引 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;
}
推荐阅读
- java - 使用 InputStreamReader 读取 URL 时出现异常
- javascript - 服务器上的语法高亮显示
- mysql - 如何使用 PHP 和 MySQL 链接帖子和评论表?
- blazor - 出现错误 发生未处理的异常。有关详细信息,请参阅浏览器开发工具。重新加载
- django - Django App Served in EBS (Elastic Beanstalk Service) using Traefik and Docker error: 502 Bad Gateway
- c# - Task.WhenAll ContinueWith 死锁
- c# - 在 C#/MySQL 中将变量值插入变量列
- odbc - 如何从 SQL Server 对象的 ODBC 17 驱动程序中读取 U/P
- javascript - 如何在条件快递js节点js中创建变量
- angular - 解决 Angular 9 中的 HttpClient 错误