2023年c語(yǔ)言筆試題_第1頁(yè)
2023年c語(yǔ)言筆試題_第2頁(yè)
2023年c語(yǔ)言筆試題_第3頁(yè)
2023年c語(yǔ)言筆試題_第4頁(yè)
2023年c語(yǔ)言筆試題_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1.填空題

(1)、請(qǐng)寫(xiě)出下列代碼的輸出內(nèi)容

#include<stdio.h>

intmain(void)

(

inta,b,c,d;

a=10;

b=a++;

c=++a;

d=10*a++;

printf("b,c,d:%d,%d,%d",b,c,d);

return0;

)

答:10,12,120

(2)charstr[]="\\\0";

char*p=str;

intn=1000;

請(qǐng)計(jì)算

sizeof(str)=____________

sizeof(p)=______________

sizeof(n)=______________

344

(3)UCHAR*pucCharArray[10][10];

typedefunionunRec

ULONGullndex;

USHORTusLevel[6];

UCHARucPos;

}REC_S;

REC_SstMax/pstMax;

四字節(jié)對(duì)齊方式時(shí):sizeof(pucCharArray)=,sizeof(stMax)=

sizeof(pstMax)=,sizeof(*pstMax)=-

40012412

(4)structBBB

{

longINum;

char*pcName;

shortsDate;

charcHa⑵;

shortsBa[6];

}*p;

p=0x100000;

p+0x1=Ox

(unsignedlong)p+0x1=Ox

(unsignedlong*)p+0x1=Ox

(char*)p+0x1=Ox

100018100001100004100001

(5)structtagAAA

unsignedcharucld:l;

unsignedcharucParaO:2;

unsignedcharucState:6;

unsignedcharucTail:4;

unsignedcharucAvail;

unsignedcharucTail2:4;

unsignedlongulData;

}AAA_S;

問(wèn):AAA_S在字節(jié)對(duì)齊分別為1、4的狀況下,占用日勺空間大小是多少?

912

(6)#pragmapack(4)/*編譯選項(xiàng),表達(dá)4字節(jié)對(duì)齊*/

intmain(intargc,char*argv[])

(

structtagTestl

(

shorta;

chard;

longb;

longc;

);

structtagTest2

longb;

shortc;

chard;

longa;

);

structtagTest3

(

shortc;

longb;

chard;

longa;

);

structtagTestlstTl;

structtagTest2stT2;

structtagTest3stT3;

printf("%d%d%d”,sizeof(stTl),sizeof(stT2),sizeof(stT3));

return0;

)

ttpragmapack()(編譯選項(xiàng)結(jié)束)

請(qǐng)問(wèn)輸出成果是:

121216

(7)enumENUMA

Xl=2,

Yl,

Zl=6,

Al,

Bl

};

enumENUM_AenumA=Yl;

enumENUM_AenumB=Bl;

請(qǐng)問(wèn)enumA=____;enumB=

38

(8)如下程序的輸出成果是一

include<stdio.h>

intfun(intxjnty)

(

staticintm=0;

staticinti=2;

i+=m+1;

m=i+x+y;

returnm;

)

voidmain()

(

intj=4;

intm=1;

intk;

k=fun(j,m);

printf(”%d,,k);

k=fun(j,m);

printf("%d\n“,k);

return;

)

817

(9)如下程序的輸出成果為

#defineCIR(r)r*r/*請(qǐng)注意這種定義歐J缺陷,不容許這樣定義*/

voidmain()

|

inta=1;

intb=2;

intt;

t=CIR(a+b);

printf("%d\n”,t);

return;

}

5

(10)structtagABC

(

charcB;

shortsC;

charcD;

longIA;

}*pAbc;

pAbc=0x100000;

那么pAbc+OxlOO=Ox;(ULONG)pAbc+0x100=Ox;(ULONG*)pAbc+

0x100=Ox;(char*)pAbc+0x100=Ox;

100C00100100100400100400

2.改錯(cuò)題

(1)下面程序把“hello"這個(gè)字符串輸出,請(qǐng)指出其中aJ錯(cuò)誤。

voidTest(void)

(

charpcArray[10];

strncpyJpcArray/'hello'^S);

printf("%s\n,pcArray);

return;

)

strncpy沒(méi)有把中斷符NULL寫(xiě)入數(shù)組中

(2)如下程序用于把"系統(tǒng)備板工作異常"字符串打印出來(lái),請(qǐng)指出其中的錯(cuò)誤:

voidPrintErrlnfo(void)

characMsg[16];

strcpy(acMsg,”系統(tǒng)備板工作異常”);

printf(“%s”,acMsg);

return;

)

每個(gè)中文占兩個(gè)字節(jié),空間局限性,字符串結(jié)尾尚有'\0’

(3)如下函數(shù)實(shí)現(xiàn)打印字符串“helloworld”日勺功能,請(qǐng)指出錯(cuò)誤:

#defineBUFFER_SIZE256

voidGetMemory(char*pszBuf)

(

if(NULL==pszBuf)

(

ASSERT(O);

return;

)

pszBuf=(char*)malloc(BUFFER_SIZE);

return;

)

voidTest(void)

(

char*pszBuf=NULL;

GetMemory(pszBuf);

if(NULL==pszBuf)

return;

)

strcpy(pszBuf,"helloworld\r\n");

printf("%s",pszBuf);

free(pszBuf);

return;

)

函數(shù)要返回指針就需要傳進(jìn)去指針的地址

(4)本題不考慮魔鬼數(shù)字問(wèn)題

voidAddFunc(unsignedinta,unsignedintb,unsignedint*c)

(

*c=a+b

)

voidmain(void)

(

unsignedchare=200;

unsignedcharf=100;

unsignedcharg=0;

AddFunc((unsignedint)e,(unsignedint)f,(unsignedint*)&g);

printf("%d”,g);

}

g是一種字節(jié)的變量,將g的地址強(qiáng)制轉(zhuǎn)換成四個(gè)字節(jié)unsignedint地址,導(dǎo)致寫(xiě)

內(nèi)存越界

(5)找出下面題目中的錯(cuò)誤

#defineID_LEN32

structSTR_A

(

charauclD[ID_LEN];

intiA;

)

structSTR_B

(

char*pauclD;

intiB;

)

//該函數(shù)將pstB內(nèi)日勺paucID指向構(gòu)造stA%IaucID

voidfuncA(structSTR_AstA,structSTR_B*pstB)

(

pstB->pauclD=stA.aucID;

}

main()

{

STR_AstA={0};

STR_BstB;

strcpy(stA.auclD,"12345");

funcA(stA,&stB);

printf("%s\n”,stB.paucID);

)

funcA傳入歐IstA歐I參數(shù)是一種值拷貝,pstB指向的I是堆棧中的I地址。

(6)指出下面程序的錯(cuò)誤

VOIDBfULONG*p)

(

*p=66*10000;

return;

}

VOIDA()

(

unsignedshorta=10*1000;

B((ULONG*)(&a));

return;

}

字符越界/溢出

7、請(qǐng)找出下面代碼中的所有錯(cuò)誤(題目不錯(cuò),值得一看)

闡明:如下代碼是把一種字符串倒序,如“abed”倒序后變?yōu)椤癲eba”

#include"string.h"

main()

char*src="hello;world";

char*dest=NULL;

intlen=strlen(src);

dest=(char*)malloc(len);

char*d=dest;

char*s=src[len];

while(len-!=O)

d++=s-;

printf(“%s”,dest);

return0;

)

答:

措施1:一共有4個(gè)錯(cuò)誤;

intmain()

(

char*src="hello,world";

intlen=strlen(src);

char*dest=(char*)malloc(len+l);〃要為分派一種空間char*d=dest;

char*s=&src[len-l];〃指向最后一種字符

while(len-!=0)

*d++=*s—;

*d=0;〃尾部要加'\0'

printf("%sn",dest);

free(dest);//使用完,應(yīng)當(dāng)釋放空間,以免導(dǎo)致內(nèi)存匯泄露

dest=NULL;〃避免產(chǎn)生野指針

return0;

)

措施2:(措施一需要額外的存儲(chǔ)空間,效率不高.)不錯(cuò)的想法

#include<stdio.h>

#include<string.h>

main()

(

H

charstr[]=hellozworld";

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論