请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
#include <iostream>
5 B1 M4 b; q. O) _/ Gusing namespace std;
7 T+ j j# e) R: |9 I W' O8 Vstruct student4 O, o8 n7 K3 q+ h
{+ X2 @1 B* y, M8 p3 C
char name[20];
* A5 G& Q; Z# t5 ^+ {& T int age;
9 M0 b5 l4 A( E4 q}; int main( )
9 S9 [- H% k! ~9 T{
3 V n+ }& t7 }9 L+ @$ \1 g, m% K student s;! y7 ]9 B* B# t& s- F. `& i. x
s.name="gyy"; //error
: C' f9 d: }' ~; w- V8 @ return 0;
$ w1 Y. F6 ~# n/ `" F7 F5 z0 Q}' n4 U; g' h& Y/ d' H0 P- O4 r
道理和以下语句错误的原因一样,数组名表示常量,不允许对常量赋值,所以常量不允许出现在“=”的左边,当做左值出现。所以不能直接用字符串赋值给数组名。但请注意:可以在定义字符数组的同时用字符串给字符数组赋初值。 char name[20]="gyy"; //ok 但先定义,再赋值的方式就是错误的。 char name[20];, s- e2 ~( R, M: X- Q1 F, r, R
name="gyy"; //error 对开始的程序修改方式(1) #include <iostream>
' K% f" Z2 w' susing namespace std;# ?3 i& I& f4 e
struct student
~% I1 u6 @& r7 |1 o- p/ ]+ A [{4 R3 t5 h! T+ A$ A; _
string name;1 w( F f( l+ o, ^5 w
int age;
; O @+ I4 e2 w4 @& {}; int main( )
% D( h' s# O a; @{
" I/ R/ f9 [: O! {- y0 i student s;
4 t) f/ G& |: s r0 x' |( Z4 T s.name="gyy"; //ok
/ `8 X/ `8 a) i! S" e5 c6 Z1 _ return 0;
9 I2 F2 s3 Z' x! ` w# ~: s} 对开始的程序修改方式(2) #include <iostream>0 h9 \8 a9 n. H5 ]3 N+ v+ G6 o
using namespace std;' A5 Y2 Y6 H6 M
struct student
# A5 x$ M: B5 o4 ^& w{
% K8 r9 ?! E. u! Z char name[20];: }0 f1 Y. a( g' W5 f) V
int age;+ p6 Y8 {" `, V( A6 q! ?
}; int main( )8 n" j# Q4 Y) F9 k1 N4 w
{
. `- ?2 V+ t0 s. R, A student s; j6 k, s8 ^# v- j! x! A
strcpy(s.name,"gyy"); //ok
) {% D: u i9 a; \% G: d3 L1 H: o9 Z return 0;
4 y) L# w' p. }0 ^# m$ a1 o+ M}
( }5 z3 c) M. i5 d9 C |