1
#include <stdio.h> struct Name {char d[11]}; int main (){ char str[11]; scanf("%s",str); struct Name new = {str}; } 

I want to initialize the Name structure new, but there is a warning: suggest braces around initialization of subobject.

How can I put the char array I read in my Name structure?

5
  • Personally I would avoid naming variables new. While it is legal to do this in C, new is a reserved keyword in C++ and it might cause confusion. Commented Mar 3, 2015 at 22:42
  • 3
    I'd encourage naming variables new as it will confound people who don't realize C++ is a different language to C (and so they might start to realize this as a result) Commented Mar 3, 2015 at 22:44
  • @MattMcNabb, please do not be encouraging bad programming practices. Commented Mar 4, 2015 at 0:05
  • @user3629249 this is a good programming practice in my opinion. new is an appropriate name for a new instance of something. Commented Mar 4, 2015 at 0:08
  • Without a length inhibition , the scanf() can/will overflow the str[] buffer, resulting in undefined behaviour. and perhaps a seg fault event. Also, the returned value from scanf (and family) should always be checked to assure the input/convert operations was successful. . Commented Mar 4, 2015 at 0:25

4 Answers 4

4

There are couple of ways:

int main () { char str[11]; scanf("%10s",str); // Make sure you don't read more than // what the array can hold. struct Name name1 = {"name"}; // This works only if you use string literal. struct Name name2; strcpy(name2.d, str); // Use this when you want to copy from another variable. } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this:

#include <stdio.h> struct Name {char d[11];}; int main (){ char str[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; scanf("%s",str); // The below is C99 style. struct Name new = { .d = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}; struct Name new2 = { .d = "hi mum"}; // Memcpy does the job as well memcpy(new.d, str, sizeof(str)); } 

EDIT:

If what you want is to copy whatever you got in your str-buffer to Name, you could do it like above. You could also do it like this.

struct Name new; scanf("%s", new.d); /* Work directly on the structure. */ 

For more info on C99-style struct initialization, check this StackOverflow-question: C struct initialization with char array

Also heed R. Sahu's warning to not read more than the array can hold.

2 Comments

What is the purpose of the 1,2,3 stuff? It seems likely that OP wants readable null-terminated strings
Just to show that there are more than one way to skin a cat. I'm searching for a good reference on C99-style struct initialization syntax. I'll edit that in my post.
0

When initialize struct in C it is a good idea to crate a function to do the initialization. I useally use a name line init_"name of struct". For you case a simple strncpy will init your string field. I use strncpy to avoid writing off the end of the string. Tip use a #define for setting the lengths of all you strings. Latter on when string lengths change you have one easy place to fix you problems. Here is you code with an init function

#include <stdio.h> #include <string.h> #include <stdlib.h> #define NAME_LEN (11) struct Name { char d[NAME_LEN]; }; void init_name(struct Name * n, char *s); // proto types should go in a h file void init_name(struct Name * n, char *s) { strncpy(n->d, s, NAME_LEN); // copy s to name file d } int main(){ char str[11]; struct Name newName; scanf("%s", str); init_name(&newName, str); } 

Comments

0
given the problems with the scanf() given the probability of a buffer overflow/undefined behaviours, etc I suggest using: #include <stdio.h> #define MAX_LEN (11) // define the struct struct Name {char d[MAX_LEN]}; // declare instance of the struct struct Name myName; int main () { // remember: char *fgets(char *str, int n, FILE *stream) if( fgets( myName.d, MAX_LEN, stdin ) ) { // then fgets successful printf( "%s\n", myName.d); } else { // else, fgets failed printf( "fgets failed\n"); } return( 0 ); } // end function: main 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.