我对这个C程序的运行方式感到困惑。我只是试图使用一个结构来存储一些变量,然后能够在一个单独的函数中打印出来。以下是我的代码。我不明白的是为什么我的第一个输出包括ip和key而不仅仅是ip。
#include <stdio.h>
struct device {
char ip[6];
char key[5];
};
void check(struct device cred);
int main ()
{
int i;
struct device cred[10];
strcpy(cred[0].ip, "192.16");
strcpy(cred[0].key, "45EA9");
check(cred[0]);
return 0;
}
void check(struct device cred)
{
printf("IP: %s\n", cred.ip);
printf("Key: %s\n", cred.key);
}
Output:
IP: 192.1645EA9
Key: 45EA9
答案 0 :(得分:0)
您分配的空间少于实际需要的空间。始终确保为NULL字符&#39; \ 0&#39;。
分配+1