phi*_*hil 1 c arrays string pointers output
int main()
{
int a = 0;
int BUFSIZE = 1000;
char *string1[20];
FILE *fp1 = fopen("input1.txt", "r");
if (fp1 == 0)
{
fprintf(stderr, "Error while opening");
return 0;
}
string1[a] = (char *)malloc(BUFSIZE);
while (fgets(string1[a], BUFSIZE, fp1)!=NULL)
{
a++;
string1[a] = (char *)malloc(BUFSIZE);
}
printf("%c", string1[3]);
}
Run Code Online (Sandbox Code Playgroud)
嗨,我得到上面的代码,它从文本文件中读取一个字符串并将其存储在一个数组中.现在我想输出数组string1的某个元素,但显然printf不起作用.此外,char*string1 [20]究竟定义了什么?它与指针有关吗?谢谢!
char *string1[20]
Run Code Online (Sandbox Code Playgroud)
声明一个名为string120个指针char的数组.因此string1[3]是格式所需的指针char,而不是指针.char%c
因为string1[3]- 如果有的话 - 填充的via fgets,它包含一个以0结尾的字符串,所以你可以使用它打印出来
printf("%s\n", string1[3]);
Run Code Online (Sandbox Code Playgroud)
如果你想打印一个字符,你可以使用
printf("%c\n", string1[3][4]);
Run Code Online (Sandbox Code Playgroud)
例如.
| 归档时间: |
|
| 查看次数: |
271 次 |
| 最近记录: |