我如何解释以下代码行

发布于 2025-01-11 12:45:55 字数 656 浏览 0 评论 0原文

typedef struct {
int a;
short s[2];
} MSG;
MSG *mp, m = {4, 1, 0};
char *fp, *tp;
mp = (MSG *) malloc(sizeof(MSG));
for (fp = (char *)m.s, tp = (char *)mp->s; tp < (char *)(mp+1);)
*tp++ = *fp++;

所以我从我的 C 编程记忆中发现,第一个块是使用 typedef 定义别名的结构声明。别名是味精? 下一个指针 *mp 和 struct m = {4, 1, 0} 被声明为 MSG 类型。 接下来创建两个字符指针:*fp 和*tp。 mp 设置为等于 malloc(sizeof(MSG)),它被强制转换为类型 (MSG *) 或指向 MSG? 的类型指针。 接下来设置一个 for 循环,其中 for fp = ms(可以说是 struct m 中的 s)被转换为指向 char 的指针,tp = 指针 mp 设置为 struct s;且 tp < (char *)(mp + 1) 或指针加 1);)

我真的不确定,因为此时我对 c 的记忆相当模糊。有人可以纠正我或者更好地阐明这段代码在做什么吗?我试图判断我是否准备好开始学习某本书,这是第一块代码。 接下来,无论指针 fp 指向什么,都会递增设置为 *tp,它也会递增。

typedef struct {
int a;
short s[2];
} MSG;
MSG *mp, m = {4, 1, 0};
char *fp, *tp;
mp = (MSG *) malloc(sizeof(MSG));
for (fp = (char *)m.s, tp = (char *)mp->s; tp < (char *)(mp+1);)
*tp++ = *fp++;

so i've sussed out from my memory of c programming that the first block is a structure declaration using typedef to define an alias. The alias is MSG?
Next pointer *mp, and struct m = {4, 1, 0} are declared as type MSG.
Next two char pointers are created, *fp and *tp. mp is set equal to malloc(sizeof(MSG)) which is cast to type (MSG *) or type pointer to MSG?.
Next a for loop is set where for fp = the m.s (the s within struct m so to speak) is cast to a pointer to a char, tp = pointer mp set to struct s; and tp < (char *)(mp + 1) or pointer plus 1);)

I'm just really unsure as my memory of c is quite foggy at this point. Could someone correct me or better articulate exactly what this code is doing? I'm trying to discern whether or not I'm ready to start studying a certain book and this is the first block of code.
Next whatever pointer fp points to is incremented set to *tp which is also incremented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2025-01-18 12:45:55

将评论转化为答案。

  • 是; MSG 是别名。
  • 是的,mp 是一个未初始化的指针,m 是一个已初始化的 MSG 结构。
  • 是的,fptpchar * 变量。
  • C 中不需要强制转换,但 C++ 中需要强制转换。
  • 然后代码或多或少地写出 memcpy() 。

可以这样写:(

memmove(mp->s, m.s, sizeof(m.s));

或者你可以使用 memcpy() - 不过我不使用它)。循环的 (char *)(mp + 1) 限制是分配的结构之后的结构开始。

代码也可以写成:

mp->s[0] = m.s[0];
mp->s[1] = m.s[1];

甚至可能更快。
事实上,它可以简单地写成:

*mp = m;

它确实初始化了 mp->a ,但无论如何也不应该长时间处于未初始化状态。如果不再使用 m 结构,则代码可以消除该变量并写入:

*mp = (MSG){ .a = 4, .s = { 1, 0 } };

使用 复合文字指定初始化器,两者都需要支持 C99 或更高版本。

有很多方法可以改进该代码片段。这让人想知道这是否是一本值得学习的好书。也许值得对书中的不恰当之处保持怀疑的态度。如果它是在上个世纪(200x)或上个千年写成的,那么对于所写的内容就有更好的借口,但是这么老的书值得一​​读吗? (那个时代的一些书仍然值得一读——请参阅下面的评论。)

Converting comments into an answer.

  • Yes; MSG is the alias.
  • Yes, mp is an uninitialized pointer and m is an initialized MSG structure.
  • Yes, fp and tp are char * variables.
  • The cast isn't necessary in C, but is in C++.
  • The code then writes out memcpy() — more or less.

It could be written:

memmove(mp->s, m.s, sizeof(m.s));

(or you could use memcpy() — I don't use that, though). The (char *)(mp + 1) limit on the loop is the start of the structure after the allocated structure.

The code could also be written as:

mp->s[0] = m.s[0];
mp->s[1] = m.s[1];

which might even be faster still.
In fact, it could simply be written as:

*mp = m;

which does initialize mp->a but that shouldn't be left uninitialized for long anyway. If the m structure isn't used again, then the code could eliminate the variable and write:

*mp = (MSG){ .a = 4, .s = { 1, 0 } };

using a compound literal and designated initializers, both of which require support for C99 or later.

There are a lot of ways to improve that code snippet. It leaves one wondering whether that is a good book to be learning from. It might be worth keeping a sceptical eye open for infelicities in the book. If it was written in the mid-oughts (200x) or in the last millennium, then there are better excuses for what was written, but is a book that old worth reading? (There are some books of that era that are still worth reading — see comments below.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文