指针引用经典笔试题
(1)
1 #include<malloc.h> 2 #include<string.h> 3 4 void GetMemory(char *p, int num) 5 { 6 p = (char *)malloc(sizeof(char) * num); 7 } 8 9 void main() 10 { 11 char *str = NULL; 12 GetMemory(str, 100); 13 strcpy(str, "hello"); 14 } 15 16 // GetMemory函数仅仅只改变了p指针的指向,而str的指向不变 17 // 类似于: 18 // (1) char *str = NULL; 19 // (2) char *p = str; 20 // (3) p = (char *)malloc(sizeof(char) * num); 21 // 那么这样的三行代码执行结果显然没有达到改变str的作用
(2)
1 #include<string.h> 2 #include<malloc.h> 3 #include<stdio.h> 4 5 void GetMemory2(char **p, int num) 6 { 7 *p = (char *)malloc(sizeof(char) * num); 8 } 9 10 void main() 11 { 12 char *str = NULL; 13 GetMemory2(&str, 100); 14 strcpy(str, "hello"); 15 printf("%s", str); 16 free(str); 17 getchar(); 18 } 19 20 // GetMemory2函数“隔山打牛” 21 // 类似于: 22 // char *str = NULL; 23 // char **p = &str; 24 // *p = (char *)malloc(sizeof(char) * num); 25 // 那么这样的三行代码执行结果显然达到改变str的作用
(3)
1 #include<string.h> 2 #include<malloc.h> 3 #include<stdio.h> 4 5 char * GetMemory3(int num) 6 { 7 char *p = (char *)malloc(sizeof(char) * num); 8 return p; 9 } 10 11 void main() 12 { 13 char *str = NULL; 14 str = GetMemory3(100); 15 strcpy(str, "hello"); 16 printf("%s", str); 17 free(str); 18 getchar(); 19 } 20 21 // GetMemory3函数功在当代,利在千秋 22 // 尽管p变量在栈中,它当生则生,当死则死 23 // 但是它在世的一瞬间,却创造了举世的成就,开天辟地!! 24 // p = (char *)malloc(sizeof(char) * num);一句足矣 25 // 返回p的值,意义无量!
(4)
1 #include<string.h> 2 #include<malloc.h> 3 #include<stdio.h> 4 5 char *GetMemory4() 6 { 7 char p[] = "hello world"; 8 return p; //编译器警告!返回局部变量 9 } 10 11 void main() 12 { 13 char *str = NULL; 14 str = GetMemory4(); 15 printf("%s", str); 16 } 17 18 // GetMemory4函数山寨版 19 // p变量在栈中,当生则生,当死则死 20 // 但是它在世的一瞬间,却目光短浅,于后世无功! 21 // 返回p的值,一文不值!
(5)
1 #include<string.h> 2 #include<malloc.h> 3 #include<stdio.h> 4 5 char * GetMemory5() 6 { 7 char *p = "hello world!"; 8 return p; 9 } 10 11 void main() 12 { 13 char *str = NULL; 14 str = GetMemory5(); 15 printf("%s", str); 16 getchar(); 17 } 18 19 // GetMemory5函数正版 20 // p变量在栈中,当生则生,当死则死 21 // 但是它在世的一瞬间,却赋予了一份常量,不随它的消失而泯灭!
(6)
1 #include<string.h> 2 #include<malloc.h> 3 #include<stdio.h> 4 5 void GetMemory6(char *&p) 6 { 7 p = (char *)malloc(sizeof(char) * 10); 8 } 9 10 void main() 11 { 12 char *str = NULL; 13 GetMemory6(str); 14 strcpy(str, "hello"); 15 printf("%s", str); 16 free(str); 17 str = NULL; 18 getchar(); 19 }
以上几个例子是面试时遇到的最频繁的试题,在此特意备份,以便学习。
【3】这道题是最典型的数组越界示例:
1 #include<iostream.h> 2 3 #define MAX 255 4 5 void main() 6 { 7 unsigned char A[MAX]; 8 for (int i = 0; i <= MAX; i++) 9 { 10 A[i] = i; 11 } 12 }
无限循环.......
【4】求最大字段和
示例代码如下:
1 #include<iostream> 2 using namespace std; 3 4 inline int Max(int a, int b) 5 { 6 return a > b ? a : b; 7 } 8 9 int MaxSubSum(int br[], int n) 10 { 11 int data = 0, max = 0; 12 for (int i = 0; i < n; ++i) 13 { 14 data = Max(0, br[i]); 15 max = Max(data + max, max); 16 } 17 return max; 18 } 19 20 void main() 21 { 22 int ar[] = {1, -4, -2, -1, 7, -3, 9}; 23 int result1 = MaxSubSum(ar, 7); 24 cout << result1 << endl; //17 25 }