Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to assign values to members of C language structure array?
How to assign values to members of C language structure array?
intmain(void){

struct students Bao = { };

printf("%d,%s\n ",bao.id,Bao . name); //The output is 4224528, which is empty (it should be null).

//structstudentsbao={3," 123 " }; Of course. The first distribution method

//strcpy(bao.name," Bao "); //Yes,

//printf("%d,%s\n ",bao.id,Bao . name);

//Bao . name = " Bao "; Error "stray'\35 1'inprogram" others are garbled.

//Bao . name[0]= ' a ';

//Bao . name[0]= '/0 ';

//printf("%d,%s\n ",bao.id,Bao . name);

/* That will do, */

//char arr[ 10]= " baobao ";

////Bao . name = arr; //Error "AssignmentToExpressionWithrayType"

//scanf("%s ",Bao . name); //Yes,

//printf("%d,%s\n ",bao.id,Bao . name);

//So you can use a function like scanf.

//and the memcpy function is also possible.

return0

}

Extended data

Direct assignment of structural array and calculation of array length in C language;

# include & ltstdio.h & gt

//Customize the structure of a string, including two variables: string and string length.

typedefstructStr{

charch[ 100];

Intlength// Length of character array (string)

} myStr

//When variables are declared for the first time, the string length of each variable is 0.

//Take an array of length 10 as an example. The array length is 1000.

//At the same time, assign 0 to the length of the 0 th to 9 th structure arrays.

myStrmyStr 1[ 10]={

[0...9]={

. Length =0,

}

};

intmain(){

inti

for(I = 0; I< 10; i++){

Printf("%d\n ",myStr 1[i]. Length);

}

return0

}