English 中文(简体)
Ugly Macro Interpretation (just 1 line)
原标题:Ugly Macro Interpretation (just 1 line)
  • 时间:2010-01-10 22:29:51
  •  标签:
  • c
#define STRING(s) (((String*)s)-1)

www.un.org/Depts/DGACM/index_french.htm

typedef 
struct String {
    int length;
    int capacity;
    unsigned check;
    char ptr[0];
} String;
最佳回答

页: 1 之后,你又从中取回一票(即指以前的任何东西)。

更具体地说,需要了解<代码>String的定义,但(WILD SPECULATION)我会猜测申请使用双型VB/C型插座(在长度之前终止),这一功能从适合C类功能(第一种特性的点名)的形式改为适用于其他类型(时间点)。

问题回答

机械地来说,宏观工程与其他企业一样已经做了描述。 但奇怪的是,您可以认为这是一种从<条码>中删除的一种形式。

<代码>String structure is the Header of acounting string, ie, one where 你知道总长度而不必逐个扫描NUL。 这一特殊版本也保留了拨款总额。 您将提出以下建议:

struct String *str = malloc(sizeof(*s) + maxlen);
str->length = 0;
str->capacity = maxlen;
str->checked = /* ??? */;

某些地方应当有一些松.的职能,以操纵这些计票。

宏观本身是从一个平原(char *)到上面分配的<代码>标准>/代码>的第一个特性,再到<代码>。 这种做法将被用于:

/* allocate str as above */
char *s = str->p;

现在,通过职能链条电话或回报,你对包含<代码><<>>>>/代码>的强硬结构有某种松动的轨道,你需要再次找到。 页: 1

String *str2 = STRING(s);

这不是在C执行计票办法的一个特别好办法,但它显示了一种人们不时看到的技术。

其他人回答了你的问题。 http://c-faq.com/struct/structhack.html http://home.datacomm.ch/t_wolf/tw/c9x_changes.html (不过,甚至在C99之前就广泛使用,而且似乎在任何地方工作)。 设想是,<条码>ptr使用0吨位,这样,如果你有点人要<条码>ptr,并且希望有一条到原来的结构,你将使用<条码>战略<>>宏观。 您正在从<代码>ptr成员地址中减去<代码>struct/code>的大小,从而获得<代码>struct的起始地址。

更好地处理<条码>truct 给其任何成员一个提示器是使用<>代码>(>) 宏观定义,载于stddef.h

#define STRING(x) ((String *)(((char *)(x) - offsetof(struct String, ptr))))

然后,你可以做到:

#include <stddef.h>
#include <stdlib.h>
#include <assert.h>

typedef struct String {
    int length;
    int capacity;
    unsigned check;
    char ptr[0];
} String;

#define STRING(x) ((String *)(((char *)(x) - offsetof(struct String, ptr))))

int main(void)
{
    String *s = malloc(sizeof *s + 100);
    String *t;
    char *mystring = s->ptr;
    t = STRING(mystring);
    assert(t == s);
    return EXIT_SUCCESS;
}

<>代码>offsetof ( is下定义,载于stddef.h

请注意,在C99中,“结构舱”将在<代码>struct内宣布<>ptr为:

char ptr[];

i.e. 无体大小。

  • (String*) = type cast to pointer to String object,
  • s = the string,
  • -1 = point to one String object s length before in the memory block

Don t 知道为什么这样作。 强权的定义显然需要这样做,但这只是一个野心。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签