English 中文(简体)
1. 与召集成员一道启动一个封套
原标题:Initializing a nested struct with const members
  • 时间:2012-05-17 13:51:02
  •  标签:
  • c

例如:

struct outer {
  struct inner {
    const int c;
    int x;
  } i;
  int y;
};

I want to malloc outer and then, later, initialize inner to get the right const behavior for outer.i.c.

例如,类似情况

struct outer *o = malloc(sizeof *o);
o->y = find_y();
int cc = find_c();
int xx = find_x();
o->i = { .c = cc, .x = xx };

但是,这给我留下了一个有关<代码>的错误,即只分配一个读写的成员,即,因为它是转让,而不是初始。

难道有办法做这样的事情,是对汇编者的冒犯? 让我们考虑trick滴与* (int *) &o->i.c)相混淆,或在&o.i上使用mcpy作为汇编者周围的污点。 我可以找到他们需要的地方,但我正在寻找最不ak的办法来这样做。

我没有使用C++(使用C99)。

问题回答

唯一实际不变的事物是只读存储的物品。 马洛丽斯/托伊斯永远不会给你只读的记忆。 因此,“const”有 lie。 这对汇编者和开发商来说也是一种吸引力。 我不敢肯定的是,在C99中是否有任何yn食糖可以处理某个人可能似乎存在的问题,但这里我会怎样做:

#include <stdio.h>
#include <stdlib.h>

struct outer {
    struct inner {
        const int c;
        int x;
    } i;
    int y;
};

int main(void)
{
    struct outer    *o;

    o      = calloc(1, sizeof(struct outer));

    printf("before: %d %d %d
", o->i.c, o->i.x, o->y);

    *((int *)&o->i.c) = 1;
    o->i.x = 2;
    o->y = 3;

    printf("after: %d %d %d
", o->i.c, o->i.x, o->y);

    return 0;
}

Please take into account that this is a more practical solution than academic, thus some people might want to throw a few rotten eggs at me for it.

我可能这样做:

struct outer_init {
  struct inner_init {
    int c;
    int x;
  } i;
  int y;
};

struct outer_init *oi = malloc(sizeof *oi);
oi->y = find_y();
oi->i.c = find_c();
oi->i.x = find_x();

struct outer *o = (struct outer *)oi;

我并不完全相信,这是绝对可行的。





相关问题
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 ...

热门标签