请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
#include <iostream>: J; b2 N( c. q& v) A4 M. ^" l
using namespace std;0 Q3 d9 J+ Z, U0 ?7 }1 r
struct student
' u: `. U7 g/ D- @7 f }! w{
: X6 ]. ^# D- d/ ? char name[20];
; Q# u$ b, A: |$ s int age;" r* [) d) Q2 k3 b! s0 I
}; int main( )
( w$ j x7 U/ |3 i{' v4 _) b1 `3 Q6 x
student s;( q% r3 `4 E1 \
s.name="gyy"; //error- d( X; _. }1 m
return 0;
8 g; P. b- q9 L. h4 `}; r$ d& {7 B; Y' [" _* h: P
道理和以下语句错误的原因一样,数组名表示常量,不允许对常量赋值,所以常量不允许出现在“=”的左边,当做左值出现。所以不能直接用字符串赋值给数组名。但请注意:可以在定义字符数组的同时用字符串给字符数组赋初值。 char name[20]="gyy"; //ok 但先定义,再赋值的方式就是错误的。 char name[20];* W# F& F0 l( O) i
name="gyy"; //error 对开始的程序修改方式(1) #include <iostream>+ b& H0 o+ j, {, }3 |: f
using namespace std;& u# J& m, Z& m! }" p. R& N/ x: u
struct student m+ K, t; w5 P' _# ] B3 u
{
5 {' Z' r7 L& C2 Y. r string name;
@" u' _( `; } Y, g8 o6 a) c int age;5 u7 z' ~3 s0 m5 f6 t
}; int main( )
; e6 f7 u- T6 J; g{( h& ^5 y2 p4 a1 N0 c& w
student s;
( l7 Q V) o$ T s.name="gyy"; //ok W/ }) s/ @- X/ s O
return 0;* M! b0 K1 L/ R% m' D. u% Z$ c" B j
} 对开始的程序修改方式(2) #include <iostream>
/ f$ W# X" G- s* Dusing namespace std;6 \- u7 J/ s1 R2 \# K5 c. q
struct student: x5 `. P+ E) s7 H
{" K! ]. y# o& u+ z+ D* T
char name[20];
! s% ~& m2 a8 z- M1 x! b( d# E int age;
, L8 J+ `; k& n" }5 K6 s$ G}; int main( )
4 g6 K+ u7 M" | Y{$ f+ X0 a# U0 w* h: [- F
student s;% r) Q4 g% }4 d( Y" [" r8 i( H
strcpy(s.name,"gyy"); //ok1 ~/ r W1 _$ v2 {5 z( }
return 0;
$ U, b. k( M9 b} 2 M4 c3 G u$ \- w/ ^) D8 A
|