从 C 中的结构返回 char 数组时出现奇怪的字符
当从 C 中的结构返回 char 数组的 printf
时,我遇到了一些问题。
struct q_entry
{
long mtype;
char mtext[MAXLENGTH + 1];
};
结构中的 long mtype
返回正常,但字符串只是返回一些奇怪的人物。
int proc_obj(struct q_entry *msg)
{
printf("\npriority: %ld name: %s\n", msg->mtype, msg->mtext);
}
它只是返回一些奇怪的字符,例如“priority: 1 name: ▒▒(”而不是“priority: 1 name: hello”
我正在使用以下代码填充结构
int enter(char *objname, int priority)
{
...
strncpy(s_entry.mtext, objname, sizeof(s_entry.mtext) - 1);
s_entry.mtype = priority;
// Send the message
if (msgsnd(s_qid, &s_entry, len, 0) == -1)
{
printf("error: msgsnd failed\n");
return(-1);
}
else
{
return(0);
}
}
我没有太多经验使用 C,所以我对使用结构不太了解,请告诉我是否需要更多上下文或部分代码,
我在上面的输入中添加了更多代码。在这里是调用 Enter 和 proc_obj 时的更多代码
main(int argc, char **argv)
{
int priority;
if (argc != 3)
{
printf("error: incorrect number of arguments\n");
exit(1);
}
else
{
priority = atoi(argv[2]);
}
if (enter(argv[1], priority) < 0)
{
printf("error: message entering failed\n");
exit(1);
}
exit(0);
}
这是与 Enter 和上面的代码位于不同的文件中
int server(void)
{
int mlen, r_qid;
struct q_entry r_entry;
// Initialize queue
if ((r_qid = init_queue()) == -1)
return(-1);
for (;;)
{
if ((mlen = msgrcv(r_qid, &r_entry, MAXLENGTH, (-1 * MAXPRIOR), MSG_NOERROR)) == -1)
{
printf("error: msgrcv failed\n");
exit(1);
}
else
{
proc_obj(&r_entry);
}
}
}
I am having some problems when returning with printf
of a char array from a struct in C.
struct q_entry
{
long mtype;
char mtext[MAXLENGTH + 1];
};
The long mtype
from the struct is returning fine, but the string is just returning some weird characters.
int proc_obj(struct q_entry *msg)
{
printf("\npriority: %ld name: %s\n", msg->mtype, msg->mtext);
}
It just returns some strange characters like "priority: 1 name: ▒▒(" and not "priority: 1 name: hello"
I am populating the struct using the following code
int enter(char *objname, int priority)
{
...
strncpy(s_entry.mtext, objname, sizeof(s_entry.mtext) - 1);
s_entry.mtype = priority;
// Send the message
if (msgsnd(s_qid, &s_entry, len, 0) == -1)
{
printf("error: msgsnd failed\n");
return(-1);
}
else
{
return(0);
}
}
I don't have much experience with C, so I don't know too much about using structs. Please let me know if more context or parts of the code is needed. Any kind of help would be very helpful.
I have added a little more code in enter above, and here is more code of the when enter and proc_obj are called
main(int argc, char **argv)
{
int priority;
if (argc != 3)
{
printf("error: incorrect number of arguments\n");
exit(1);
}
else
{
priority = atoi(argv[2]);
}
if (enter(argv[1], priority) < 0)
{
printf("error: message entering failed\n");
exit(1);
}
exit(0);
}
This is in a different file from enter and above code
int server(void)
{
int mlen, r_qid;
struct q_entry r_entry;
// Initialize queue
if ((r_qid = init_queue()) == -1)
return(-1);
for (;;)
{
if ((mlen = msgrcv(r_qid, &r_entry, MAXLENGTH, (-1 * MAXPRIOR), MSG_NOERROR)) == -1)
{
printf("error: msgrcv failed\n");
exit(1);
}
else
{
proc_obj(&r_entry);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码中唯一明显的错误是,您应该在 s_entry.mtext[MAXLENGTH] 处显式填充零,这样,如果 strncpy() 达到限制,字符串仍将以零结尾。但如果这就是问题所在,您会看到“hello”后面跟着奇怪的字符。您确定 objname 指向您期望它指向的文本吗?
另外,proc_obj() 声明返回 int 但实际上不返回任何内容,这看起来有点奇怪。你的编译器应该对此抱怨。
The only obvious error in your code is that you should explicitly fill in a zero at
s_entry.mtext[MAXLENGTH]
such that the string will still be zero terminated if strncpy() hits the limit. But if that were the problem, you would see "hello" followed by strange characters. Are you sure thatobjname
points to the text you're expecting it to point to?Also, it looks a bit strange that proc_obj() is declared to return an int but actually does not return anything. Your compiler ought to complain about that.
在添加更多代码之前回答
看起来
s_entry
结构对象是本地的,而enter
作用于本地变量。完成初始化后,如何调用enter
并返回结构?请注意,您将int
作为enter
函数的返回类型。如果您正在执行return s_entry;
那么您得到的输出是可能的,因为只有结构的第一个单词,即sizeof (int)
的较低部分>mtype 被考虑。如果您使用如上所述的
enter
函数,则将enter
的返回类型设置为struct s_entry
您应该检查
enter
的大小code>len 发送消息时。Answer before adding more code
It looks like the
s_entry
structure object is local, andenter
works on the local variable. How are you callingenter
and returning the structure after you have done initializing it ? note that you haveint
as the return type of theenter
function. If you are doingreturn s_entry;
then the output you are getting is possible, as only the first word of the structure, ie the lowersizeof (int)
part ofmtype
is considered.If you are using
enter
function like this as i described above then make the return type ofenter
tostruct s_entry
You should check the size of
len
when sending the message.您没有显示消息队列调用,但我的猜测是您以某种方式错误调用了 API 并将垃圾放入队列中(然后在服务器中打印垃圾)。
You don't show the message queue call but my guess is you are somehow miscalling the API and putting garbage into the queue (and then printing garbage in the server).