Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I declare strings within C structs?

Hello I am new to this site, and I require some help with understanding what would be considered the "norm" while coding structures in C that require a string. Basically I am wondering which of the following ways would be considered the "industry standard" while using structures in C to keep track of ALL of the memory the structure requires:

1) Fixed Size String:

typedef struct
{
    int damage;
    char name[40];
} Item;

I can now get the size using sizeof(Item)

2) Character Array Pointer

typedef struct
{
    int damage;
    char *name;
} Item;

I know I can store the size of name using a second variable, but is there another way?

i) is there any other advantage to using the fixed size (1)

char name[40];

versus doing the following and using a pointer to a char array (2)?

char *name;

and if so, what is the advantage?

ii) Also, is the string using a pointer to a char array (2) going to be stored sequentially and immediately after the structure (immediately after the pointer to the string) or will it be stored somewhere else in memory?

iii) I wish to know how one can find the length of a char * string variable (without using a size_t, or integer value to store the length)

like image 834
user3791487 Avatar asked Jun 30 '14 19:06

user3791487


People also ask

How do you declare a string in a struct?

typedef struct string { char *text; } *string; Then I will malloc() each one of them when it is required to make new data of the string type (array of char).

Can you put a string in a struct?

The answer is yes unless you are using an obsolete compiler that does not support initialization of structures with string class members. Make sure that the structure definition has access to the std namespace.

How do we declare string in C?

Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

Can you declare in a struct?

Declaration. The general syntax for a struct declaration in C is: struct tag_name { type member1; type member2; /* declare as many members as desired, but the entire structure size must be known to the compiler.


2 Answers

There are basically 3 common conventions for strings. All three are found in the wild, both for in-memory representation and storage/transmission.

  1. Fixed size. Access is very efficient, but if the actual length varies you both waste space and need one of the below methods to determine the end of the "real" content.
  2. Length prefixed. Extra space is included in the dynamically allocation, to hold the length. From the pointer you can find both the character content and the length immediately preceding it. Example: BSTR Sometimes the length is encoded to be more space efficient for short strings. Example: ASN-1
  3. Terminated. The string extends until the first occurrence of the termination character (typically NUL), and the content cannot contain that character. Variations made the termination two NUL in sequence, to allow individual NUL characters to exist in the string, which is then often treated as a packed list of strings. Other variations use an encoding such as byte stuffing (UTF-8 would also work) to guarantee that there exists some code reserved for termination that can't ever appear in the encoded version of the content.

In the third case, there's a function such as strlen to search for the terminator and find the length.

Both cases which use pointers can point to data immediately following the fixed portion of the structure, if you carefully allocate it that way. If you want to force this, then use a flexible array on the end of your structure (no pointer needed). Like this:

typedef struct
{
    int damage;
    char name[]; // terminated
} Item;

or

typedef struct
{
    int damage;
    int length_of_name;
    char name[];
} Item;
like image 158
Ben Voigt Avatar answered Sep 24 '22 08:09

Ben Voigt


1) is there any other advantage to using the fixed size (1)

char name[40];

versus doing the following and using a pointer to a char array (2)?

char *name;

and if so, what is the advantage?

With your array declared as char name[40]; space for name is already allocated and you are free to copy information into name from name[0] through name[39]. However, in the case of char *name;, it is simply a character pointer and can be used to point to an existing string in memory, but, on its own, cannot be used to copy information to until you allocate memory to hold that information. So say you have a 30 character string you want to copy to name declared as char *name;, you must first allocate with malloc 30 characters plus an additional character to hold the null-terminating character:

char *name;
name = malloc (sizeof (char) * (30 + 1));

Then you are free to copy information to/from name. An advantage of dynamically allocating is that you can realloc memory for name if the information you are storing in name grows. beyond 30 characters. An additional requirement after allocating memory for name, you are responsible for freeing the memory you have allocated when it is no longer needed. That's a rough outline of the pros/cons/requirements for using one as opposed to the other.

like image 27
David C. Rankin Avatar answered Sep 24 '22 08:09

David C. Rankin