c - 结构和代码解释

标签 c pointers struct printf naming

我正在为期中考试学习,但我在学习这个类(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%d3.14 来自规范的第一部分,%.2lf,它表示,需要一个长 float ,以 2 位精度显示数字。有关 printf 格式的更多信息,请参见维基百科:https://en.wikipedia.org/wiki/Printf_format_string

159 来自 u.q.a(这是它指向的内容),并由 %d 在规范结束。它最初被赋予 R 结构,它具有 int a。它成为 S 结构 fq 部分。

关于c - 结构和代码解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28746402/

相关文章:

c++在不知道扩展类的情况下调用存储在基类变量中的扩展类的构造函数

c++ - C/C++ 将 void* 转换为 int ( * () ) (int,...);

arrays - Swift - 将结构数组传递给另一个结构的初始化

c++ - 将堆上的结构从 C++ 转换为 C

c - 了解由于 getchar 而导致的 c 循环

c++ - 预定义的宏来识别调用函数?

c - libclang : error: clang-c/Index. h: 没有那个文件或目录

c++ - FFmpeg av_read_frame 和最大数据包大小

c - 如何根据大小取消引用?

c - 带有字符串指针的结构