user654077
user654077

Reputation: 43

struct initialization failure

I'm having difficulty initializing a struct in code per below. Can this even be done, or do I really need to memcpy (urg) the 5-character string into the struct?

struct MyStruct  
{  
    char x[5];  
};

main(...)  
{  
    const char* MyString = {"mnopq"}; // I understand this is a non-NULL terminated string -  
                                  // it's OK, I just want five character fields in an array

struct MyStruct = {MyString};     // <---This gives warnings below  
}

warning: missing braces around initializer
warning: initialization makes integer from pointer without cast

If I wrap initializer like:

struct MyStruct = {{MyString}};

the first warning goes away. The second warning does not. And, thus, the struct is not initialized as hoped.

Thanks in advance for help.

Upvotes: 0

Views: 767

Answers (4)

DavidMFrey
DavidMFrey

Reputation: 1668

You are trying to fill a char array with a pointer. MyString is seen as a const char * instead of an array. Change your type in the structure to const char *x, and it should do the trick. Also, MyString is actually a NULL terminated string here. The compiler sets it into memory with the extra NULL byte at the end, then treats MyString like a const char *.

If you do want the char x[5] instead of the const char *, you could initialize in a couple of ways,

struct MyStruct ms = {{'m', 'n', 'o', 'p', 'q'}};

or you could do

struct MyStruct ms = {{ MyString[0], MyString[1], MyString[2], MyString[3], MyString[4]}};

The first set of braces is for initializing the struct members, the second set is for initializing the char array members, so each member(char) has to be set individually.

Upvotes: 3

Rob
Rob

Reputation: 4141

The answer by DavidMFrey is correct and should get credit before mine! However, here's the source code using his answer as I was able to run it:

#include <stdio.h>
struct MyStruct  
{  
    char x[5];  
};
int main(void)  
{  
    const char* MyString = "mnopq";
    struct MyStruct foo = {{ MyString[0], MyString[1], MyString[2], MyString[3], MyString[4]}};
    printf("foo.x is: %s\n", foo.x); // outputs: foo.x is: mnopq¦ah¦"

    struct MyStruct ms = {{'m', 'n', 'o', 'p', 'q'}};
    printf("ms.x is: %s\n", ms.x);   // ms.x is: mnopq

    return 0;
}

Upvotes: 0

Erik
Erik

Reputation: 91260

"mnopq" is an array, const char * MyString is not.

struct MyStruct foo = {"abcde"}; will work, whereas your approach converts the pointer MyString to an integral value and assigns it to the first element of x.

Upvotes: 4

Cristian Toader
Cristian Toader

Reputation: 96

You need to declare a variable of the type MyStruct and assign it's x value to that string. This should work:

struct MyStruct {
char x[5];
};

int main(int argc, char *argv[]) {

   struct MyStruct a;
   const char* MyString = "mnop";

   strncpy((a.x), MyString, 5);

   /* 
    *if within the 5 characters you do not have the null char it will 
    * also print garbage
    */
   printf("%s", a.x);

   return 0;
}

However you can't assign a pointer to const char to an array of char as they are different types. So you would need to either use char* in the struct or use memcpy / strcpy to get the information.

Upvotes: 0

Related Questions