#include <stdio.h>

struct poly {
        int len;
        int arr[];
        char *name;
};


int main() {
        int i;
        struct poly p;
        p.len = 45;
        p.arr[3] = {1,1,1};
        p.name = "Josh";

        printf("%d",p.len);
        for(i = 0; p.arr[i] != NULL ; i++)
                printf("%d",p.arr[i]);
        printf("%s",p.name);
        return 0;
}

I tried to make a simple program to see if I understood structures. Apparently, I do not. Haha.

I am getting these error messages when I compile:
struct.c:5: error: flexible array member not at end of struct
struct.c: In function âmainâ:
struct.c:14: error: expected expression before â{â token

Does anyone mind helping me learn what I did wrong and how to fix it?

Thank you!

Try:

struct poly {
        int len;
        char *name;
        int arr[];
};

BTW, your structure won't alway take up the same number of bytes. That might be fine for a particular situation.

Flexible array members are a C feature introduced in C99 whereby one can declare the last element to be an array of unspecified size.

#include <stdio.h>

struct poly {
        int len;
        int arr[3]; /* this must have a defined size, three for example */
        char *name;
};


int main() {
        int i;
        struct poly p;
        p.len = 45;
        /* p.arr[3] = {1,1,1}; p.arr[3] is only one integer, you can not load three into it */
       /*
       * you can do the following
       */
        p.arr[0] = 1;
        p.arr[1] = 1;
        p.arr[2] = 1;


        p.name = "Josh"; /* careful here, "Josh" is read only memory, it can not be modified */

        printf("%d",p.len);
       /* for(i = 0; p.arr[i] != NULL ; i++)  p.arr[i] != NULL doesn't make sense here since p.arr[i] is an integer and NULL is a pointer 
                printf("%d",p.arr[i]); */
       /*
        * This is what you are trying to do
        */
        for (i = 0; i < sizeof(p.arr) / sizeof (p.arr[0]); i++) {
              printf("%d", p.arr[i]);
        }
        printf("%s",p.name);
        return 0;
}

Read the comments I added to your source code.

Of course if you were looking to assign the values of the structure poly p at declaration time, then it would have been:

struct poly p = { 45, { 1, 1, 1 }, "Josh" };
#include <stdio.h>

struct poly {
        int len;
        int arr[];
        char *name;
};


int main() {
        int i;
        struct poly p;
        p.len = 45;
        p.arr[3] = {1,1,1};
        p.name = "Josh";

        printf("%d",p.len);
        for(i = 0; p.arr[i] != NULL ; i++)
                printf("%d",p.arr[i]);
        printf("%s",p.name);
        return 0;
}

I tried to make a simple program to see if I understood structures. Apparently, I do not. Haha.

I am getting these error messages when I compile:
struct.c:5: error: flexible array member not at end of struct
struct.c: In function âmainâ:
struct.c:14: error: expected expression before â{â token

Does anyone mind helping me learn what I did wrong and how to fix it?

Thank you!

It is wrong to specify int arr[]. In an one dimensional array you should specify the size of the array. ex: int arr[5]. Then in int main specify int main(void) and include conio.h header before using return 0; use getch(); try these rectifications.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.