3.4.34. Zero length arrays

You can use zero length arrays. These might be used for a header on a malloced data structure, for example:

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

typedef struct {
    int length;
    char letters[0];
    } linet;

linet* Zero_Length_0(char *arg)
{
    linet * linep = malloc (sizeof (linet) + strlen(arg) + 1 );
    strcpy (linep->letters, arg);
    return linep;
}

char * aString="this is a simple string";
int main() {
  printf("String: '%s'\n", Zero_Length_0(aString)->letters);
  return 0;
}

Supported in GNU mode only.

Copyright © 2002-2005 ARM Limited. All rights reserved.ARM DUI 0205F
Non-Confidential