[英]Character Pointer to strings
This is C code snippet: 这是C代码段:
int main()
{
char *names=[ "tom", "jerry", "scooby" ];
printf("%s", *names[0]);// prints t
printf("%s", *names[1]);// prints j
// how to print full word "tom", or full word "jerry"
}
As stated earlier, I want my output to be: tom jerry scooby So using pointers how do I print the whole thing? 如前所述,我希望输出为:tom jerry scooby因此,如何使用指针打印整个内容?
Does it compile? 它可以编译吗? Because your array initialization is not correct.
因为您的数组初始化不正确。 To properly declare an array and print them, you can do:
要正确声明一个数组并打印它们,您可以执行以下操作:
#include <stdio.h>
int main(void)
{
char *names[]= { "tom", "jerry", "scooby" };
printf("%s %s %s\n", names[0], names[1], names[2]);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.