2

What actually happened in following program, I have defined an empty array int arr[]; in code and with the GCC compiler, but the compiler doesn't give an error. It's successfully worked.

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

typedef struct st
{
        int i;
        int arr[];
}ST,*ptr;

int main()
{
        ST s1;
        ptr p1= (ptr)malloc(sizeof(ST)+4*sizeof(int));

        p1->i=10;
        p1->arr[0]=1;
        p1->arr[3] = 1;

        printf("%d\n",p1->arr[3]);
        printf("%ld\n", sizeof(s1));
}

C language not allowed undefined array length. but GCC compiler allowed. Why?

Just curious, what actually happens?

5
  • 5
    C standard allows this. Search for "flexible array member". Commented Aug 25, 2016 at 8:40
  • 5
    Flexible array member, two suggestions: don't cast malloc and don't hide pointers with typedefs Commented Aug 25, 2016 at 8:40
  • As the last member of the struct, it is useful when the struct stores a variable amount of data. Commented Aug 25, 2016 at 8:41
  • 2
    @AlterMann Thank you sir, I not aware this topic.. Commented Aug 25, 2016 at 8:42
  • 2
    It's a C99 feature; not available in out-of-date C compilers (notably some versions of Microsoft Visual Studio). Commented Aug 25, 2016 at 8:45

3 Answers 3

3

It's valid and allowed since C99. It's called flexible array member - useful to make the last member of the struct variable-length and it must be the last member of the struct if used.

Sign up to request clarification or add additional context in comments.

Comments

2

You are looking at a C99 feature, Flexible array member. If the array is defined at the end of the struct, then you will define its size during allocation via malloc, by allocating a bigger chunk of memory.

for an array of size len:

struct st *mySt = malloc(sizeof(struct st) + len * sizeof(mySt->arr[0]));

Comments

1

C99, the structure of the last element allows the size of the array is unknown, which is called a flexible array member, but in front of the structure of a flexible array member must have at least one other member.

Flexible array members allowed to structure contains an array of variable size. This size of the structure sizeof returns do not include a flexible array of memory. Structure comprising a flexible array member using malloc () function allocates memory dynamically and allocates memory should be larger than the size of the structure to accommodate the expected size of the flexible array.

The basic model

typedef struct st
{
        int i;
        int arr[];
}ST,*ptr;

Tested under linux gcc about operating results for the structure, the last element of an array, and an array of undefined length, operating results consistent with theory, this is a bit like a c ++ class member functions, member functions is not occupied class space size. Well, this flexible array, there is the role of what is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.