在C中,当我创建任何n元组并尝试使用它时,我只能使用它的最后一个元素.即使是类型显然是元组的最后一个元素而不是元组本身.我怎样才能获得除最后一个元素之外的元素?我快速浏览了一下规格,没有看到它.
例:
#include <stdio.h>
int f() {return 2;}
char* g() {return "dudebro";}
int main() {
printf("%d\n", (f(),g(),3)); /* Should print the address of the tuple (unless it's by-value, in which case it should be a compile error) but prints the last element?*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行:
$ gcc -ansi -pedantic -Wall -Wextra tt.c -o tt
$ ./tt
3
Run Code Online (Sandbox Code Playgroud)
在C中没有元组这样的东西.你在那里使用了逗号运算符.
如果要在C中整理相关数据,则需要定义和使用结构.如果要打印出所有数据项,则需要printf对每个数据项进行显式调用(或使用多个格式说明符).
例如
typedef struct Foo {
int a;
char *b;
};
Foo foo;
foo.a = 5;
foo.b = "hello";
printf("%d %s\n", foo.a, foo.b);
Run Code Online (Sandbox Code Playgroud)