好的,它是hw的一部分,我不能使用字符串来解决我的问题。我有点卡住了,因为我需要使用这些字符数组。关于如何解决这个问题,有什么建议吗?
int main()
{
struct structure
{
char name[15];
};
structure ObrLog[2]=
{
{"Bambi"},
{"Cindarella"},
};
ObrLog[1].nazwa="somethingnew"; //error here
}发布于 2011-11-02 04:51:35
要将C字符串复制到缓冲区中,请使用memcpy。假设你指的是name而不是nazwa
char newval[] = "somethingnew";
// in a function
memcpy(ObrLog[1].name, newval, min(strlen(newval) + 1, sizeof(ObrLog[1].name));
ObrLog[1].name[14] = '\0'; // just to be sure the name is NUL-terminatedmin的定义留给读者作为练习。+1用于解释newval末尾的隐式NUL。
发布于 2011-11-02 04:47:02
您不能像那样修改字符数组。为此,您需要使用strcpy,并且要注意缓冲区溢出。(假设输入错误为nazwa而不是name )
strcpy(ObrLog[1].name, "somethingnew");https://stackoverflow.com/questions/7972650
复制相似问题