2020年3月19日

指针数组:实际上是数组,其元素都是指针;如:int *p[3];

数组指针:实际是指针,指向数组。如:int (*p)[3]:指向数组的指针,该数组有3个int型数据。

【】的优先级要比 *  高 ,同时int *p[3] 等价于 int *(p[3])


nt *p[4] 与选择项中的 说明 () 等价,

正确答案: C   你的答案: D (错误)

int p[4]
int *p
int *(p[4])
int (*p)[4]

基类 
继承方式 public protected private
public public    protected 不可继承
private private private 不可继承
protected protected protected 不可继承

基类的公有成员是其私有派生类的(   )成员

正确答案: B   你的答案: D (错误)

不可见
私有
保护
公有


空格处应填写()。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <iostream>

using namespace std;

class A

{

    public:

        int m;

        int* p;

};

int main()

{

    A s;

    s.m = 10;

    cout<<s.m<<endl; //10

    s.p = &s.m;

    () = 5;

    cout<<s.m<<endl; //5

    return 0;

}

正确答案: D   你的答案: D

s.p = 5
s->p = 5
s.*p = 5
*s.p = 5

.的运算符优先级比*要高,*s.p=*(s.p)。

B、C看起来似乎很对,但他们的调用对象必须是类对象的指针,而不能是类对象。

以下程序运行后的输出结果是(      )
int main()
{
int a=1,b=2,m=0,n=0,k;

k=(n=b<a)&&(m=a) ;
printf("%d,%d\n",k,m);

return 0;
}

正确答案: A   你的答案: A

0,0
0,1
1,0
1,1

考察逻辑运算符的短路特性,m=a在本例中不会执行!

下面程序的输出结果是(      )

1

2

3

4

5

6

7

8

9

#include <stdio.h>

int main()

int  intArray[] = {1, 2, 3, 4, 5}; 

int  *p = (int *)(&intArray+1); 

printf("%d,%d",*(intArray+1),*(p-1)); 

return 0; 

}


 

正确答案: D   你的答案: D

1,5
1,6
2,4
2,5

intArray:是数组的首地址,

&intArray:就是存放这个数组首地址的地址,可用int (*)[5]的指针保存,

&intArray+1:相当于int (*)[5]这个指针的指针偏移,偏移量是指向元素的大小*1,(比如double *p,p+1的偏移量就是一个double的大小*1)

(int *)(&intArray+1):相当于把偏移后的地址(也是一个int[5])强转给p保存;

以下能对二维数组a进行正确初始化的语句是()。

正确答案: B   你的答案: D

int a[2][ ]={{1,0,1},{5,2,3}};
int a[ ][3]={{1,2,3},{4,5,6}};
int a[2][4]={{1,2,3},{4,5},{6}};
int a[ ][3]={{1,0,1},{},{1,1}};

定义二维数组并赋初值时,可以省略第一维的大小,但不能省略第二维的大

,所以A是错误的;

对于C来说,定义的数组a是2行4列的数组,但赋初值却包含了

3行,所以是错误的;

D中初值列表中有一行是空的,这在C语言中是不允许的,所以也

是错误的;

B定义了2行3列的二维数组并对其赋初值,是正确的,所以正确答案是B.

设已有定义:char*st="how are you”;,下列程序段中正确的是()。

正确答案: A   你的答案: D

char a[11],*p;strcpy(p=a+1,&st[4]);
char a[11];strcpy(++a,st);
char a[11];strcpy(a,st);
char a[ ],*p;strcpy(p=a[1],st+2);

a是数组名,是一地址常量,++a是非法的,所以答案B错误;

因为数组a的大小为11,执行strcpy(a,st);,st字符串的结尾符’\0’无法存放到数组a中,所以答案C也是

错误的;

答案D中定义数组a时未指定大小,所以也是错误的;

对于答案A来说,执行strcpy(p=a+1,&st[4]);,首先将数组a的第二个元素a[1]的地址赋给指针变量p,然后将st第5个

元素开始直到字符串结束为止的字符复制到p所指向的内存单元中,所以选择答案是A。

下面程序的输出结果是(      )

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

下面程序的输出结果是(      )

#include <iostream>

using namespace std;

int main() 

{

    char str1[] = "hello world";  

    char str2[] = "hello world";  

    const char str3[] = "hello world";  

    const char str4[] = "hello world";  

    const char* pstring1 = "hello world";  

    const char* pstring2 = "hello world";  

    cout << boolalpha << ( str1==str2 ) <<  ',' 

    cout << boolalpha << ( str3==str4 ) << ',' ;  

    cout << boolalpha << ( pstring1==pstring2 ) <<endl;

    return 0;

}


 

正确答案: A   你的答案: A

false,false,true
false,false,false
true,true,true
false,true,true

#include<iostream>

using namespace std;

int main(void)

{

    char str1[] = "hello world";   //存放在栈中的数组

    char str2[] = "hello world";   //存放在栈中的数组

    const char str3[] = "hello world";  //存放在栈中的字符串常量

    const char str4[] = "hello world";  //存放在栈中的字符串常量

    const char* pstring1 = "hello world";   //本身在栈中,指向常量的指针

    const char* pstring2 = "hello world";   //本身在栈中,指向常量的指针     //显然二者所指向的地址一致

    int x = (int)pstring1;

    int y = (int)pstring2;                  //为了方便打印出指针所指向的地址

    cout << boolalpha << ( str1==str2 ) << endl;               //比较字串首地址      flase

    cout << boolalpha << ( str3==str4 ) << endl;               //比较字串首地址      flase

    cout << boolalpha << ( pstring1==pstring2 ) <<endl;        //比较指针所指地址    true

    cout << "str1=" << &str1 << ",";

    cout << "str2=" << &str2 << endl;

    cout << "str3=" << &str3 << ",";

    cout << "str4=" << &str4 << endl;

    cout << "pstring1=" << &pstring1 << ",";

    cout << "pstring2=" << &pstring2 << endl;                   //输出指针本身地址

    cout<<hex;

    cout << "pstring1=" << x << ",";

    cout<<hex;

    cout << "pstring2=" << y << endl;      //16进制输出指针所指地址

    return 0;

}

发布了225 篇原创文章 · 获赞 140 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/jankin6/article/details/104880307