1.有以下程序段:
struct st {
int x;
int *y;
};
int a[]= {1,2},b[]= {3,4};
st c[2]= {10,a,20,b};
st *pt;
pt=c;
以下选项中表达式值为11的是:A 【正确答案: A】
A ++pt->x //成员选择运算符的优先级大于自加运算符,指向10再加1,11.
B pt->x //10
C *pt->y //成员选择运算符的优先级大于取值运算符,先指向a,再取值1。
D (pt++)->x //后自加,先用后加,运行效果相当于pt->x.结果为10.
另 (++pt)->x //指针后移后取值,20.
2.有以下程序
#include <iostream>
using namespace std;
struct S {
int a, b;
} data[2] = { { 10, 100 }, { 20, 200 } };
int main() {
S *p = data + 1;
++p->a;
cout << data[1].a;
return 0;
}
程序运行后的输出结果是:D 【正确答案: D】
A 10
B 11
C 20
D 21
//p指向data[1],再指向a,再加1,20变为21。
3.若有以下程序段:
struct dent{
int no;
int *m;
};
int a=1,b=2,c=3;
dent s[3]={{101,&a},{102,&b},{103,&c}};
int main(){
struct dent *p;
p=s;
…
return 0;
}
则以下表达式中值为 2 的是: A【正确答案: D】
A (p++)->m //p->m;&a;
B *(p++)->m //*p->m;a;1
C (p).m //s[0].m;&a;
D (++p)->m //(s[1])->m;&b;2;
4.若有以下定义,
struct AA {
int a;
char b;
};
则对结构体数组初始化正确的是:C【正确答案: C】
A AA s[2]={10,”a”;20,”b”}
B AA s[2]={{10,”a”},{20,”b”}}
C AA s[2]={{10,‘a’}, {20,‘b’}} //单引号,逗号。
D AA s[2]={{10,”a”}{20,”b”}}
5.若有以下定义,
struct BB {
int a;
char b[5];
};
int main() {
……
BB s[2] = { { 10, "read" }, { 20, "write" } };
……
return 0;
}
则值‘read’的表达式是:C【正确答案: B】
A s.b
B s[0].b
C s[1].b
D s[2].b
6.若有以下定义,
#include <iostream>
using namespace std;
struct st
{
int x;
int *y;
} *p;
int dt[4]={ 10,20,30,40 };
st aa[4]={{50,&dt[0]},{60,&dt[0]},{60,&dt[0]},{60,&dt[0]}};
int main(){
p=aa;
cout<<++(p->x);
return 0;
}
以下程序的输出是:C【正确答案: C】
A 10
B 11
C 51 //50+1=51;
D 60
7.读程序,写结果:
#include <cstring>
#include <iostream>
using namespace std;
struct write {
char name[10];
int age;
};
struct book {
char title[20];
double price;
write author;
} a[] = { { "Turbo C", 28.3, { "xia", 53 } }, { "C++", 36, { "gong", 22 } }, {
"Pascal", 29.6, { "hu", 35 }
}, { "VB5.0", 41.7, { "gao", 38 } }
};
int search(char *name2) {
int i;
for (i = 0; i < 4; i++)
if (strcmp(name2, a[i].title) == 0)
return (i);
return -1;
}
int main() {
int i;
char name1[20];
for (i = 0; i < 4; i++)
cout << a[i].title << '\t' << a[i].author.name << endl;
cout << "Input search name:";
cin.getline(name1, 20);
if ((i = search(name1)) >= 0)
cout << a[i].title << ' ' << a[i].price << endl;
else
cout << "Title='" << name1 << "'not found!" << endl;
return 0;
}
若程序运行时输入“C++”,则程序输出为 C++ 36 【 正确答案: C++ 36】
若程序运行时输入“Fortran”,则程序输出为 Title=Fortran not found! 【 正确答案: Title='Fortran’not found!】
//=后面和not前面有一个单引号。
8.下面程序用于查找学生姓名为name的学生,并输出其三门课成绩。请将程序补充完整。
#include <iostream>
#include <cstring>
using namespace std;
struct Score{
char name[20];
double score[3];
};
int main() {
Score ss[5]={"zhang",{90,80,98},"cheng",{78,88,89},"wang",{88,98,76},"lin",{98,88,78},"wu",{68,89,97}};
char name[20];
cin>>name;
int i;
for(i=0;i<5;i++){
if( strcmp(name,ss[i].name)==0 //【 正确答案: strcmp(name,ss[i].name)==0 或 strcmp(ss[i].name,name)==0】){
for(int j=0;j<3;j++){
cout<< ss[i].score[j] //【 正确答案: ss[i].score[j] 或 (*(ss+i)).score[j]】<<" ";
}
break;
}
}
return 0;
}
9.读程序,写结果: wang 88 【 正确答案: wang 88】
#include <iostream>
using namespace std;
struct Score {
char name[20];
double score[3];
};
int main() {
Score ss[5]= {"zhang",{90,80,98},"cheng",{78,88,89},"wang",{88,98,76},"lin",{98,88,78},"wu",{68,89,97}};
Score *p;
p=ss+2; //&ss[2]
cout<<p->name<<" "<<p->score[0];
return 0;
}
10.结构数组中存有三人的姓名和年龄,以下程序输出三人中年龄最年长者的姓名,请补充完整。
#include <iostream>
using namespace std;
struct man {
char name[20];
int age;
} person[] = { { "liming", 18 }, { "wanghua", 19 }, { "zhangping", 20 } };
int main() {
int old = 0,i;
for (i=0; i <3; i++) {
if( person[i].age>person[old].age // 【 正确答案: person[old].age < person[i].age 或 person[i].age>person[old].age 或 (*(person+old)).age<(*(person+i)).age 或 (*(person+i)).age>(*(person+old)).age】)
{
old=i; //old 得到age最大的元素的下标。
}
}
cout<< ~~person[i].name~~ // 【 正确答案: person[old].name 或 (*(person+old)).name】;
return 0;
}
11.读程序,写结果: 20 15 【 正确答案: 20 15】
#include <iostream>
using namespace std;
struct Demo{
int x;
Demo *pt;
};
int main() {
Demo tt[4]={20,tt+1,15,tt+2,30,tt+3,17,tt};
int i;
Demo *p;
p=tt;
for(i=1;i<=2;i++){
cout<<p->x<<" "; //第一次循环:20 第一次循环:15
p=p->pt;
}
return 0;
}
12.读程序,写结果: Z18 【 正确答案: Z18】
#include <iostream>
using namespace std;
struct stu {
int num;
char name[10];
int age;
};
void fun(struct stu *p) {
cout << (*p).name[0]<<p->age; //name的第一个字符。
}
int main() {
stu students[3] = { { 9801, "Zhang", 20 }, { 9802, "Wang", 19 }, {9803, "Zhao", 18 } };
fun(students + 2); //&students[2]
return 0;
}
13.读程序,写结果: 50 30 【 正确答案: 5030】
#include <iostream>
using namespace std;
struct st {
int x;
int *y;
};
int main() {
int dt[4] = { 10, 20, 30, 40 };
st aa[4];
for (int i = 0; i < 4; i++) {
aa[i].x = 50;
aa[i].y = dt + i; //&dt[0],&dt[1],&dt[2],&dt[3],
}
st *p;
p=aa+2; //&aa[2]
cout<<p->x<<*p->y; //5030
return 0;
}
14.结构数组中存有三人的姓名和年龄,以下程序输出三人中年龄最年长者的姓名,请补充完整。
#include <iostream>
using namespace std;
struct man {
char name[20];
int age;
} person[] = { { "liming", 18 }, { "wanghua", 19 }, { "zhangping", 20 } };
int main() {
man *p, *q;
int old = 0;
p = q = person;
for (; ~~p < person+2~~ ; p++) //【 正确答案: person+3 或 &person[3]】 0,1,2.
{
if (old < p->age) {
q = p; //q存入地址。
old=p->age //【 正确答案: old=p->age】;
}
}
cout<< ~~q.name~~ //【 正确答案: q->name】 //q是指针,应用指针对应的指向成员运算符。
return 0;
}
15.读程序,写结果: Zhao 【 正确答案: Zhao】
#include <iostream>
using namespace std;
struct stu {
int num;
char name[10];
int age;
};
void fun(struct stu *p) {
cout << (*p).name;
}
int main(void) {
struct stu students[3] = { { 9801, "Zhang", 20 }, { 9802, "Wang", 19 }, {9803, "Zhao", 18 } };
fun(students + 2); //&student[2]
return 0;
}
16.读程序,写结果: 60 70 80 90 【 正确答案: 60 70 80 90】
#include <iostream>
using namespace std;
struct st {
int x;
int *y;
};
void fun(st *aa, int n) {
for (int i = 0; i < n; i++) {
*aa[i].y = aa[i].x + *aa[i].y; //改变aa[i].y指向的值。50+10,50+20,50+30,50+40.即60,70,80,90.
}
}
int main() {
int dt[4] = { 10, 20, 30, 40 };
st aa[4];
for (int i = 0; i < 4; i++) {
aa[i].x = 50;
aa[i].y = dt + i; //&dt[0],&dt[1],,&dt[2],,&dt[3]
}
fun(aa, 4);
for (int i = 0; i < 4; i++) {
cout << *aa[i].y << " ";
}
return 0;
}