输出这个程序:
#include<stdio.h> #include<stdlib.h> int fun(int x, int y) { static int m = 0; static int i = 2; i += m + 1; m = i + x + y; return m; } void main() { int j = 4; int m = 1; int k; k = fun(j, m); printf("%d\n", k); k = fun(j, m); printf("%d\n", k); system("pause"); return 0; }
结果:
8
17
指出程序错误:
(1)
unsigned long FUNC_B(unsigned long ulCount) { unsigned long ulSum = 0; while (0 <= ulCount)//无符号ulCount范围>=0,while一直为真,跳不出来,导致程序死循环 { ulSum += ulCount; ulCount--; } return ulSum; }
(2)
char*GetStr(char*p)//p是str的一份拷贝 { p = "hello world";//只把字符串的首地址h传给了函数(保存在了p里,并没有保存在str里)所以什么都没有输出 return p; } void main() { char*str = NULL; if (NULL != GetStr(str)) { printf("\r\n str=%s", str); } } 改之后: char*GetStr(char**p) { *p = "hello world"; return *p; } void main() { char*str = NULL; if (NULL != GetStr(&str)) { printf("\r\n str=%s", str); } }
(3)
void VarInit(unsigned char*pucArg)改正:将char改为long { *pucArg = 1; return; } void Test() { unsigned long ulGlobal;//4个字节 VarInit((unsigned char*)&ulGlobal);//ulGlobal传给pucArg,但是pucArg是char*类型,只访问了1个字节,后面3个字节不知,所以不能输出1 printf("%lu", ulGlobal); return; }
(4)
LONG A() { if (条件1) { return;//无返回值,错误 } return VOS_OK; } VOID B() { if (A()) { DoSomeThing1(); } else { DoSomeThing2(); } return; }
(5)
#define ID_LEN 32 struct STR_A { char aucID[ID_LEN]; int iA; }; struct STR_B { char*paucID; int iB; }; void funcA(struct STR_A stA, struct STR_B*pstB) { pstB->paucID = stA.aucID; } void main() { STR_A stA = { 0 }; STR_B stB; strcpy(stA.aucID, "12345"); funcA(stA, &stB); printf("%s\n", stB.paucID); }
(6)
#define MAX_SIZE 255 void main() { unsigned char buff[MAX_SIZE + 1]; unsigned char i; for (i = 0; i <=MAX_SIZE; i++)//i为无符号,范围0-255,i<=255恒成立,跳不出来,死循环 { buff[i] = i; } }
(7)
............... gui_push_clip(); #ifdef AAA//条件AAA定义,if参与,若AAA无定义则if不参与 if (show_status==MMI_TRUE) #endif #ifdef BBB gui_show_image(x, y, image_id); #endif gui_pop_clip(); update_dt_display(); ................ 有隐患,AAA定义了则上面成立,若没定义则底下BBB(隐藏)成立,只需要一个,所以去掉BBB
时间: 2024-10-26 05:34:44