Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String literal inside struct - C

Tags:

c

I need to encapsulate a literal string in a struct. The code below won't compile, but hopefully illustrates what I would like to do?

struct my_struct
{
char* str = "string literal";
};
like image 774
Maslow Leif Avatar asked Feb 21 '11 22:02

Maslow Leif


People also ask

How do you initialize a string in a struct?

You have to initialize the struct when you create an instance of the struct. struct my_struct { char* str; }; int main(int argc,char *argv[]) { struct my_struct foo = {"string literal"}; ... }

Can you return a string literal in C?

It's perfectly valid to return a pointer to a string literal from a function, as a string literal exists throughout the entire execution of the program, just as a static or a global variable would.

How is a string literal stored in C?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'. Do not confuse the null byte, '\0', with the character '0', the integer 0, the double 0.0, or the pointer NULL.

Where are literal strings stored?

The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character. A null character (represented by the \0 escape sequence) is automatically appended to, and marks the end of, each string literal.


2 Answers

You can't initialize any members in a struct declaration. You have to initialize the struct when you create an instance of the struct.

struct my_struct
{
   char* str;
};

int main(int argc,char *argv[]) 
{
  struct my_struct foo = {"string literal"};
  ...
}

Since you want the str member to refer to a string literal, you'd better make it a const char *str , as you can't modify string literals any way.

Alternatively

Provide an initialization function

to initialize your struct to a known state every time.

struct my_struct
{
   const char* str;
   int bar;
};

void init_my_struct(strut my_struct *s)
{
   s->str = "string literal";
   s->bar = 0;
}
int main(int argc,char *argv[]) 
{
  struct my_struct foo;
  init_my_struct(&foo);

Or initializing it using the preprocessor:

struct my_struct
{
   const char* str;
   int bar;
}; 

#define MY_STRUCT_INITIALIZER {"string literal",0}
int main(int argc,char *argv[]) 
 {
   struct my_struct foo = MY_STRUCT_INITALIZER;

Or copy from a known object:

struct my_struct
{
   const char* str;
   int bar;
};
const struct my_struct my_struct_init = {"string_literal",0};

int main(int argc,char *argv[]) 
{
   struct my_struct foo = my_struct_init;
like image 194
nos Avatar answered Oct 31 '22 05:10

nos


You'll have to create an instance of the struct, and then set the str member. And if you plan to use string literals with it, you really should change it to const char* str.

struct my_struct
{
    const char* str;
};

int main() {
    struct my_struct s1;
    s1.str = "string literal";

    /* or */
    struct my_struct s2 = {"string literal"};
}
like image 24
user470379 Avatar answered Oct 31 '22 03:10

user470379