




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1.填空題
(1)、請寫出下列代碼的輸出內容
#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;
請計算
sizeof(str)=____________
sizeof(p)=______________
sizeof(n)=______________
344
(3)UCHAR*pucCharArray[10][10];
typedefunionunRec
ULONGullndex;
USHORTusLevel[6];
UCHARucPos;
}REC_S;
REC_SstMax/pstMax;
四字節(jié)對齊方式時: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;
問:AAA_S在字節(jié)對齊分別為1、4的狀況下,占用日勺空間大小是多少?
912
(6)#pragmapack(4)/*編譯選項,表達4字節(jié)對齊*/
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()(編譯選項結束)
請問輸出成果是:
121216
(7)enumENUMA
Xl=2,
Yl,
Zl=6,
Al,
Bl
};
enumENUM_AenumA=Yl;
enumENUM_AenumB=Bl;
請問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/*請注意這種定義歐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.改錯題
(1)下面程序把“hello"這個字符串輸出,請指出其中aJ錯誤。
voidTest(void)
(
charpcArray[10];
strncpyJpcArray/'hello'^S);
printf("%s\n,pcArray);
return;
)
strncpy沒有把中斷符NULL寫入數組中
(2)如下程序用于把"系統(tǒng)備板工作異常"字符串打印出來,請指出其中的錯誤:
voidPrintErrlnfo(void)
characMsg[16];
strcpy(acMsg,”系統(tǒng)備板工作異?!?;
printf(“%s”,acMsg);
return;
)
每個中文占兩個字節(jié),空間局限性,字符串結尾尚有'\0’
(3)如下函數實現打印字符串“helloworld”日勺功能,請指出錯誤:
#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;
)
函數要返回指針就需要傳進去指針的地址
(4)本題不考慮魔鬼數字問題
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的地址強制轉換成四個字節(jié)unsignedint地址,導致寫
內存越界
(5)找出下面題目中的錯誤
#defineID_LEN32
structSTR_A
(
charauclD[ID_LEN];
intiA;
)
structSTR_B
(
char*pauclD;
intiB;
)
//該函數將pstB內日勺paucID指向構造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參數是一種值拷貝,pstB指向的I是堆棧中的I地址。
(6)指出下面程序的錯誤
VOIDBfULONG*p)
(
*p=66*10000;
return;
}
VOIDA()
(
unsignedshorta=10*1000;
B((ULONG*)(&a));
return;
}
字符越界/溢出
7、請找出下面代碼中的所有錯誤(題目不錯,值得一看)
闡明:如下代碼是把一種字符串倒序,如“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個錯誤;
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);//使用完,應當釋放空間,以免導致內存匯泄露
dest=NULL;〃避免產生野指針
return0;
)
措施2:(措施一需要額外的存儲空間,效率不高.)不錯的想法
#include<stdio.h>
#include<string.h>
main()
(
H
charstr[]=hellozworld";
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 職業(yè)熱情測試題及答案
- 幼兒園時間與數字課堂試題及答案
- 小學教師教育策略展望試題及答案
- 宜賓遴選筆試試題及答案
- 微分積分考試題及答案
- 幼兒園考試卷及答案語文
- 游仙期末考試試卷及答案
- 藝考水彩試題大全及答案
- 理解風險管理中的商務英語應用技巧研究試題及答案
- 一年級下期未試卷及答案
- 教師語言與溝通藝術知到智慧樹章節(jié)測試課后答案2024年秋溫州大學
- 《路基養(yǎng)護》課件
- 《基于EVA的科大訊飛企業(yè)價值評估的計算過程及結果探析案例報告》10000字(論文)
- 空氣輸送斜槽選型手冊
- 服裝IE(浙江紡織服裝職業(yè)技術學院)知到智慧樹答案
- 培訓機構教務管理崗位職責
- 水利工程項目法人質量責任追究和獎懲制度
- DB44-T 2537-2024 小型水電站退役導則
- 托幼機構安全應急預案制度
- 酒店執(zhí)行力培訓
- 企業(yè)合規(guī)之內控與風險管理
評論
0/150
提交評論