不,从 6.9 开始,vxWorks C shell 不支持结构。请参阅Wind River Workbench Host Shell 用户指南的C 解释器限制部分。shell 使用一个不跟踪其符号类型的符号表,并且您不能在命令行上定义类型。
要获得相同的行为,您可以为结构分配内存,然后按地址填充它。您分配的大小不必精确,但您的内存偏移量可以。您的示例很简单,因为您只有 2 个指针,并且您的结构不会有任何填充。如果您使用的是 32 位处理器,则指针的大小为 4 个字节。所以第一个指针位于偏移量 0、1、2 和 3,然后第二个指针从偏移量 4 开始。
例如,
-> mystruct = malloc(50);
New symbol "mystruct" added to kernel symbol table.
mystruct = 0x3a320a0: value = 63949520 = 0x3cfcad0
-> mystruct[0] = "hi";
0x3cfcad0: value = 61030776 = 0x3a364a0
-> mystruct[4] = "hello";
0x3cfcae0: value 61040928 = 0x3a36920 = ' '
-> printf("%s\n", mystruct[4]);
hello
value = 6 = 0x6
当你的结构变大时,如果有填充,事情就会变得棘手。例如,struct { char, int }
大小可能为 6 或 8,具体取决于处理器的对齐要求,因此在尝试获取int
. 如果它变得太复杂,你可以在你的代码中编译可能看起来像这个伪代码的辅助函数:
struct st { char a; int b; char *c; };
struct st *make_me_a_struct(char a, int b, char *c)
{
struct st *newstruct = malloc(sizeof(st));
newstruct->a = a;
newstruct->b = b;
newstruct->c = c;
return newstruct;
}