我正在为期中考试学习,但我在学习这个类(class)时遇到了困难。
在 int main (void)
中,第一个 printf
应该打印 Jello,3.14159
我按照它如何打印 Jello,3.14
而不是 159 - u.q.a
指向哪里?
/* 01234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ */
#include <stdio.h>
#include <string.h>
struct R {
char n[6];
int a;
};
struct S {
double p;
struct R q;
};
struct S f(const struct R* x) {
struct S a;
a.q = *x;
a.p = 3.142;
return a;
}
char g(struct R* y) {
strcpy_s(y->n, "Wee");
y->a = 3;
return *y->n;
}
int main(void) {
char c;
struct S u;
struct R x = { "Jello", 159 };
u = f(&x);
printf("%s,%.2lf%d\n", u.q.n, u.p, u.q.a);
c = g(&x);
printf("%s,%d.142,%cee\n", x.n, x.a, c);
x.n[0] -= 3;
x.n[1] = 'r';
printf("%seeeeeeeeee\n", x.n);
return 0;
}
最佳答案
the first printf should print "Jello,3.14159" i follow how it prints the Jello,3.14 but not the 159...
和
where does " u.q.a" point to?
两者一起回答。这是相关的输出:
3.14159
来自printf格式:%.2lf%d
。 3.14
来自规范的第一部分,%.2lf
,它表示,需要一个长 float ,以 2 位精度显示数字。有关 printf
格式的更多信息,请参见维基百科:https://en.wikipedia.org/wiki/Printf_format_string
159
来自 u.q.a
(这是它指向的内容),并由 %d
在规范结束。它最初被赋予 R
结构,它具有 int
a
。它成为 S
结构 f
的 q
部分。
关于c - 结构和代码解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28746402/