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;

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章