Home > Back-end >  New problems
New problems

Time:11-27

#include

Struct MyStruct
{
int age;
Char sex;
Char name [100].
};

Int main (void)
{
Struct MyStruct st.
St. age=18;
St. name="ha ha";
='M' st. sex;

If (st. sex=='M')
Age: printf (" % d gender: male name: % s \ n ", st. age, st. name);
The else
Age: printf (" % d gender: female name: % s \ n ", st. age, st. name);

return 0;
}
Why the compile error? VC6.0=always convert from 'char [5]' to 'char [100]' you cannot convert?

CodePudding user response:

Array cannot be assigned, st. name="ha ha"; Is no good, use strcpy ()

CodePudding user response:

Upstairs said to also wrong, if it is a single array definitions to initialize can be assigned, such as char name [50]="ha ha" can be
But if not initialized in definition, initialized then later all can't, such as char name [50]; Name="haha" is not can
And your array structure, and is equivalent to the second, good have been defined, but there were no initialization, so if you use is right the
Struct MyStruct st (18, "haha", "M")

CodePudding user response:

String=equal to symbol assignment with a pointer type brackets is an array type

CodePudding user response:

 # include 
#include //- add strcpy () function header file -

Struct MyStruct
{
int age;
Char sex;
Char name [100].
};

Int main (int arg c, char * argv [])
{
Struct MyStruct st.
St. age=18;
Strcpy (st. name, "ha ha");//- use strcpy () function to copy -- -- -- -- -- -- --
='M' st. sex;

If (st. sex=='M')
Age: printf (" % d gender: male name: % s \ n ", st. age, st. name);
The else
Age: printf (" % d gender: female name: % s \ n ", st. age, st. name);
return 0;
}

Vc + + 6.0 c + + environment debugging results

CodePudding user response:

1, the definition of time directly use string assignment
Char a, [10]="hello".//sizeof (a) for 10
Or char a []="hello"; Is 6//sizeof (a)
Note: cannot be defined first, then assigned to it, such as char a, [10]. A [10]="hello"; This is wrong!

CodePudding user response:

Used in the structure using a pointer, do not recommend using array, especially for embedded development

CodePudding user response:

Name is an array, "ha ha" is a string, two content stored in different areas, the c language does not support this directly assigned to a string array of writing, only according to the element in the array to write the characters, the writing only are effective in defining an array, is known as the initialization, but write this initialization if in words, of this structure may not be as topic main wish,
  • Related