简体   繁体   中英

How to declare string in C structs?

I just started working with structs and I ran into this problem where it gives me the following error that I don't know how to tackle:

Flexible array member 'name' with type 'char []' is not at the end of struct

I found out that making the chars as pointers or giving them a size fixes the error but I'm not sure I'm doing the right thing.

struct car{
    char company[];
    char model[];
    int price;
    int hp;
    char fuel_type[];
    char color[];
};

The above doesn't work but this does:

struct car{
    char company[20];
    char model[20];
    int price;
    int hp;
    char fuel_type[20];
    char color[20];
};

Same thing with pointers. So, what and how should I use to fix this?

Only one member of a struct (the last member) can be an incomplete array type (an array with an unspecified size, aka a "flexible array"). This is because, if there were to be other such members, which are of unknown size at compile time, the compiler could not determine the required offsets from the structure's start to any members that occur after such a member.

Here is what this (Draft) C11 Standard has to say:

6.7.2.1 Structure and union specifiers

...
18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a. (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Note, also, that structures with such a flexible array member cannot appear as array elements or (nested) members of other structures.

for Arrays like name[] you have to assign them data immediately in C else it won't work, like

int arr[]={2,4,5};

for using arrays in struct use pointer and dynamically allocate them or use arrays with fixed sizes like

char *arr;
char arr[20];

you need a fixed array size.

As alternativ you can use Pointer like "char *strString" but you need to malloc or calloc memory size (string size) in running time. In that case the size of the string can be flexible.

    #define BUF 255
   char *str = NULL;
   char Buffer[BUF];

   printf("String? ");
   fgets(Buffer, BUF, stdin);
   str = malloc(strlen(Buffer)+1);
   if(NULL == str) {
      printf("No memory");
      return EXIT_FAILURE;
   }
   strcpy(str, Buffer);
   free(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM