简体   繁体   English

C中的结构和数组问题

[英]struct and array problem in C

Here is the problem: 这是问题所在:
I have a struct like this: 我有一个这样的结构:

struct{
    Variable a;
    Variable 2;
    char ch[1];
}

I need to point ch to another struct containing several char arrays. 我需要将ch指向另一个包含几个char数组的struct No, I CAN NOT change the first struct definition at all. 不,我完全不能更改第一个结构定义。 I just need some how to put the first byte of my second struct in ch[1] and I simply don't know how to do this. 我只需要一些如何将第二个struct的第一个字节放入ch[1]而我根本不知道该怎么做。 Please help me. 请帮我。 Thanks. 谢谢。

You can't "point" an array at something else because an array is not a pointer. 您不能将数组“指向”其他对象,因为数组不是指针。 An array of one char is just an array in which you can store a single char value. 一个char数组只是一个可以在其中存储单个char值的数组。

If you can't change the definition from an array to a pointer then you can't make it "point". 如果不能将定义从数组更改为指针,则不能使其为“点”。 I'm afraid it's as simple as that. 恐怕就这么简单。

This is a very common paradigm in C. The last char is just a placeholder for variable data, and in reality you will allocate a larger buffer and overrun the 1 character. 这是C语言中非常常见的范例。最后一个char只是变量数据的占位符,实际上,您将分配更大的缓冲区并超出1个字符。 This is perfectly legal. 这是完全合法的。

You cannot change the struct but when you call malloc() to allocate it you allocate some extra bytes. 您不能更改该结构,但是在调用malloc()进行分配时,会分配一些额外的字节。

What you need in place of the char[1] is a pointer (to the other struct). 您需要代替char[1]是一个指针(指向其他结构)。

Let us give your struct a name: 让我们给您的结构命名:

typedef struct{
    Variable a;
    Variable 2;
    char ch[1];
} Element;

in your code: 在您的代码中:

Element * elt = malloc( sizeof( Element ) + sizeof( CharArrayStruct* ) - 1);
 /* fill your struct */
*(CharArrayStruct **)elt.ch = &myCharArrayStruct;
/* later on reading it back */
CharArrayStruct * pcas = *(CharArrayStruct **)elt.ch;

Note elt.ch is the address of the start of an array, not a char. 注意elt.ch是数组开头的地址,而不是char。 Note that its contents are an OtherStruct* therefore it must be cast to CharArrayStruct** 请注意,其内容是OtherStruct*因此必须将其OtherStruct*OtherStruct* CharArrayStruct**

You could come up with a third struct : 您可以提出第三个struct

struct c {
    struct a *a;
    struct b *b;
    struct c *next;
}

Append a new one of these to a head each time you want to store an association from your struct a to your struct b -- when you need to look one up, walk down the linked list, doing pointer comparisons on .a until you find the matching one, and return the .b . 每次要存储从struct astruct b的关联时,请将其中一个添加到头部-当您需要向上查找时,向下走链表,在.a上进行指针比较,直到找到匹配的那个,并返回.b

Don't forget to remove these entries from the linked list when you free() struct a objects, and don't forget to NULL the .b members when you free() struct b objects. 当您使用free() struct a对象时,不要忘记从链接列表中删除这些条目,并且当您使用free() struct b对象时,也不要忘记将.b成员设为NULL

I don't think it's possible to do in a safe way, at least not unless you're in some low-memory environment. 我认为不可能以安全的方式进行操作,至少除非您处在低内存环境中,否则至少不可能这样做。 A single char (which is what your ch field is) only consists of 1 byte; 单个字符(这就是您的ch字段)仅包含1个字节; this is simply not enough bits to point to any memory location in your virtual memory space. 这根本不足以指向虚拟内存空间中的任何内存位置。 Even if you cast that char to (void *) you won't be able to address the vast, vast majority of your memory with it. 即使将char(void *) ,也无法使用它来处理绝大多数内存。

Perhaps you should step back and tell us what you're actually trying to accomplish, rather than what you think you need to do in order to accomplish it. 也许您应该退后一步,告诉我们您实际要实现的目标,而不是告诉您要实现目标所需要做的事情。

If you mean what you literally say in the actual question body ("I just need some how to put the first byte of my second struct in ch[1] and I simply don't know how to do this."), and I disregard the first sentence that talks about "pointing", you could do it like this: 如果您的意思是您在实际问题正文中所说的话(“我只需要一些如何将第二个结构的第一个字节放入ch[1]而我根本不知道该怎么做。”),然后我忽略谈论“指向”的第一句话,您可以这样进行:

struct{
    Variable a;
    Variable 2;
    char ch[1];
} my_struct_that_cant_be_changed;

struct my_other_struct x;

my_struct_that_cant_be_changed.ch[0] = *(char *) &x;

This simply reads the first char from the other struct (in the variable x ) and assigns that value into the 1-character array of the first struct. 这只是从另一个结构(在变量x )读取第一个char ,并将该值分配给第一个结构的1个字符的数组。

Note that this seems completely pointless to me, but it does (sort of) what you want. 请注意,这对我来说似乎毫无意义,但确实可以满足您的要求。

First, it's not clear what you want to do. 首先,不清楚您想做什么。 It's not possible to "point" to another struct using a one-byte value. 不可能使用一个字节的值“指向”另一个结构。 Secondly, it doesn't make sense to place the first byte of the second struct in ch[0] without doing anything else. 其次,将第二个结构的第一个字节放在ch [0]中没有做任何其他事情是没有意义的。

The way the struct is constructed I suspect that that it is designed to be a variable length struct . 我怀疑该结构的构造方式是可变长度的结构 (Other answers have touched this, but they use the extra space to store a pointer, not the entire string.) (其他答案触及了这个问题,但是它们使用多余的空间来存储指针,而不是整个字符串。)

By allocating some extra bytes, you would get the following layout in memory: 通过分配一些额外的字节,您将在内存中获得以下布局:

+----
| Variable a
| Variable 2 (sic)
| ch[0]
+------ Extra memory below:
| ch[1]
| ...
| ch[N]
+------

You can allocate this by: 您可以通过以下方式分配:

p = malloc(sizeof(Element) + N);

You can access the element like p->ch[4] and access ch as a string using p->ch . 可以访问像元件p->ch[4]和访问ch如使用字符串p->ch

Now, it's up to you to fill your ch array with the string (or whatever) you want. 现在,由您决定要用所需的字符串(或其他任何字符串)填充ch数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM