[英]Initialize constant pointer to array in same struct
typedef struct {
char a[100];
char const *secondHalfOfA;
}
//Can i set { secondHalfOfA = a[49]; } somehow?
Sure you can. 你当然可以。
typedef struct {
char a[100];
char const *const secondHalfOfA;
} toto;
#define TOTO_INITIALIZER(NAME) { .secondHalfOfA = &((NAME).a[49]) }
static toto A = TOTO_INITIALIZER(A);
Using initializers like that consistently has the extra plus that it initializes your array with all 0
, too. 始终如一地使用这样的初始化器具有额外的优点,那就是它也用全0
初始化了数组。
This is for C99 with designated initializers. 这适用于具有指定初始化程序的C99。 If you only have historical C you could do something like 如果您只有历史C,则可以执行以下操作
#define TOTO_INITIALIZER(NAME) { { 0 }, &((NAME).a[49]) }
Edit: Seeing your comment on another answer, I suppose that you had the const
in your type on the wrong side. 编辑:看到您对另一个答案的评论,我想您输入错误的类型就是const
。 But real C initialization works very well for that case, too. 但是在这种情况下,真正的C初始化也非常有效。
You cannot automatically initialize a pointer to the middle of an object. 您不能自动初始化指向对象中间的指针。
You can, however, pretend to have overlapping objects. 但是,您可以假装有重叠的对象。 It's not pretty though :-) 虽然不是很漂亮:-)
#include <stdio.h>
#include <string.h>
struct twoparts {
union {
char a[20];
struct {
char dummy[10];
char b[10];
} s;
} u;
};
int main(void) {
struct twoparts x;
strcpy(x.u.a, "1234567890123456789");
printf("second part: %s\n", x.u.s.b);
return 0;
}
No. You can't do that in a typedef
. 不,您不能在typedef
做到这一点。 Remember: with typedef
keyword you are defining data types, not variables. 请记住:使用typedef
关键字定义的是数据类型,而不是变量。
In C++ you can do that in the default constructor. 在C ++中,您可以在默认构造函数中执行此操作。 But in C, there is no way to do that. 但是在C语言中,没有办法做到这一点。
Is Pablo said , you can't do that with an initializer. Pablo说过 ,您不能使用初始化程序来做到这一点。 You can do it easily enough after initialization, but it has to be every time: 初始化后,您可以很容易地做到这一点,但是每次都必须这样做:
// Definition (once)
typedef struct {
char a[100];
char const *secondHalfOfA;
} TYPENAME;
// Use (each time)
TYPENAME t;
t.secondHalfOfA = &t.a[49];
// Or
TYPENAME *pt;
pt = malloc(sizeof(*pt));
pt->secondHalfOfA = &pt->a[49];
It's for reasons like this that we have object oriented languages like C++ (and Java and ...) and their associated constructors , so that we can create structures with custom initializations reliably. 出于这样的原因,我们拥有面向对象的语言(例如C ++(以及Java和...)及其关联的构造函数) ,以便我们可以可靠地创建具有自定义初始化的结构。
Demonstration program: 示范程序:
#include <stdio.h>
// Definition (once)
typedef struct {
char a[100];
char const *secondHalfOfA;
} TYPENAME;
int main(int argc, char* argv[])
{
// Use of the type (need to initialize it each time)
TYPENAME t;
t.secondHalfOfA = &t.a[49];
// Example actual use of the array
t.a[49] = 'A';
printf("%c\n", *t.secondHalfOfA); // Prints A
// Uncommenting the below causes a compiler error:
// "error: assignment of read-only location ‘*t.secondHalfOfA’"
// (That's gcc's wording; your compiler will say something similar)
//*t.secondHalfOfA = 'B';
//printf("%c\n", *t.secondHalfOfA);
return 0;
}
Compilation and output using gcc: 使用gcc编译和输出:
$ gcc -Wall temp.c $ ./a.out A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.