Question: Using pure C (and the C-preprocessor) I would like to create a string buffer that includes a length parameter, so that I could pass it around easily and any functions that operate on it could do so without danger of writing past the end. This is simple enough:

```
typedef struct strbuf_t_ {
    int  length;
    char str[1];
} strbuf_t;

```

but then if I want a small scratch space on the stack to format some output text, there is no trivial way to allocate a `strbuf_t` on the stack. What I would like is some clever macro that allows me to do:

```
STRBUF(10) foo;
printf("foo.length = %d\n", foo.length); // outputs: foo.length = 10
strncpy(foo.str, "this is too long", foo.length);

```

Unfortunately I don't seem to be able to do that. The best I've come up with is:

```
#define STRBUF(size, name) \
    struct {\
        strbuf_t buf;\
        char space[size - 1];\
        char zero;\
    } name ## _plus_space_ = { .buf={.length=size}, .space="", .zero='\0'};\
    strbuf_t *name = &name ## _plus_space_.buf

int main(void)
{
    STRBUF(10, a);

    strncpy(a->str, "Hello, world!", a->length);
    printf("a->length = %d\n", a->length); // outputs: a->length = 10
    puts(a->str); // outputs: Hello, wor
}

```

This meets all of the requirements I listed, but `a` is a pointer not the structure itself, and the allocation is certainly not intuitive.

Has anyone come up with something better?

Answer: 
You are using a string to index the list, `'0'` is a string, not an integer. Remove the quotes:

```
print(data['cosponsors'][0]['thomas_id'])

```

When in doubt, check the *partial* result; see what `print(type(data['cosponsors']))` produces; if that produces `<type 'list'>`, you know you need to use indexing with integers, if you get `<type 'dict'>`, use keys (a list of which can be gotten by calling `print(list(...))` on the dictionary), etc.

Usually, lists contain a variable number of objects; it could be just one, zero or a whole load more. You could *loop* over those objects:

```
for cosponsor in data['cosponsors']:
    print(cosponsor['thomas_id'])

```

The loop sets `cosponsor` to each of the objects in the `data['cosponsors']` list, one by one.
How about 

```
data['cosponsors'][0]['thomas_id']

```

Since a list has numeric indices.