当使用%s时,无法访问数组的第一个下标,但可能与%c一起使用

当使用%s时,无法访问数组的第一个下标,但可能与%c一起使用,c,arrays,char,c-strings,C,Arrays,Char,C Strings,在使用%s时无法访问数组[0],但在使用%c时能够访问数组[0],请帮助我找到解决方案。在使用%s==>&数组[0]时应使用地址 因为%s需要一个指针作为参数 通常我们使用 #include <stdio.h> #include <string.h> void main() { char array[]="hello"; printf("%s",array[0]); printf("%c",array[0]); } 这里字符\数组\名称是第一个元素的地址 cha

在使用%s时无法访问数组[0],但在使用%c时能够访问数组[0],请帮助我找到解决方案。

在使用%s==>&数组[0]时应使用地址

因为%s需要一个指针作为参数

通常我们使用

#include <stdio.h>
#include <string.h>
void main()
{
char array[]="hello";
printf("%s",array[0]);  
printf("%c",array[0]);  
}
这里字符\数组\名称是第一个元素的地址

character_array_name==&character_array_name[0]

如果只想打印一个字符,则需要使用

printf("%s",character_array_name);
示例代码:


虽然你已经接受了一个答案,但我认为我的答案在某种程度上可以帮助你

字符串文字是一个字符序列,您可以像下面这样可视化数组:

#include<stdio.h>
int main()
{
char *str="Hello World";
printf("%s\n",str); //this prints entire string and  will not modify the  pointer location  but prints till the occurence of Null.

printf("%.1s\n",str); //only one character  will be printed
printf("%.2s\n",str); //only two characters will be printed
//initially str points to H in the "Hello World" string, now if you increment str location
str++;  //try this str=str+3;
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

str=str+5; //prints from the W
printf("%s\n",str); //this prints upto Hello because scanf treats space or null as end of strings
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

return 0;
}
printf是一个变量,在您指定它的参数之前,它对它的参数一无所知,因此当它看到%c格式说明符时,它假定下一个参数将是一个存储字符的变量,在这种情况下,它的数组[0]即字符h存储在数组的索引0处

现在,当printf看到一个%s时,它假设下一个参数将是一个指针,指向您希望它打印的字符串文字hello,在这种情况下,数组[0]不是指针,您应该将数组放在printf中,请注意数组名称不是指针,而是指针


此外,您应该使用int mainvoid代替void main它的标准

您对%c和%s的理解是什么?您想做什么?打印一个完整的字符数组,然后打印一个字符?我对使用%c和%sthanx@gangadhar进行下标访问感到困惑,那么您可以用一些例子解释另一个问题,例如prgm,以显示%s如何保存对应的地址string@Dineshdk%s没有保留该地址。这是标准库函数中的格式说明符,地址由参数保存,该参数在%s用于printf等函数时传递,scanf等。请参阅添加的示例。@Dineshdk让我知道它是否对您有帮助:您可以帮助我探索使用%s执行ptr操作的差异,但culdn无法获得wi shuld i use int mainvoid?@Dineshdk您应该使用它,因为它是C标准:-。。。。但请确保你有最新的。。。C11标准已经出台。查看以下帖子:-@Dineshdk详细说明main的返回类型始终为int而不是void,参数处的void表示main不会接受任何参数,您正在开始编程,总有一天您将制作软件,因此确保您的程序是可移植的非常重要,并且只有在您遵守以下标准的情况下才能实现:
#include<stdio.h>
int main()
{
char *str="Hello World";
printf("%s\n",str); //this prints entire string and  will not modify the  pointer location  but prints till the occurence of Null.

printf("%.1s\n",str); //only one character  will be printed
printf("%.2s\n",str); //only two characters will be printed
//initially str points to H in the "Hello World" string, now if you increment str location
str++;  //try this str=str+3;
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

str=str+5; //prints from the W
printf("%s\n",str); //this prints upto Hello because scanf treats space or null as end of strings
printf("%.1s\n",str); //only one character
printf("%.2s\n",str); //only two characters will be printed

return 0;
}
         +---+---+---+---+---+----+
  array: | h | e | l | l | o | \0 | 
         +---+---+---+---+---+---+-
           ^
           | 
          array[0]