嵌入式面试题1

[csharp]  view plain copy
  1. 1. 输出什么?  
  2. int a = (1,2);  
  3. printf("a=%d\n",a);  
  4. 答:a=2,逗号表达式的赋值  
  5. 2. struct value {  
  6.     char a[3];  
  7.     short b;  
  8. };  
  9. struct value temp;  
  10. printf("sizeof(temp) is %d\n"sizeof(temp));  
  11. 答:sizeof(temp) is 6  
  12. 3.编写程序交换a,b的值(使用二种方法)  
  13. void swap(int *a, int *b)  
  14. {  
  15.     int temp;  
  16.     temp = *a;  
  17.     *a = *b;  
  18.     *b = temp;  
  19. }  
  20.   
  21. void swap(int *a, int *b)  
  22. {  
  23.     *a = *a + *b;  
  24.     *b = *a - *b;  
  25.     *a = *a - *b;  
  26. }  
  27.   
  28. 4. 说明int *p[5]和int(*p)[5]的区别  
  29. 答:int *p[5]是指针数组,数组里存储的是指针  
  30. (*p)[5]是数组指针,指向一个含有5个数的数组  
  31. 5. 编写函数实现链表的创建,节点的插入和删除  
  32. typedef struct node  
  33. {  
  34.     int num;  
  35.     struct node *next;  
  36. }Node;  
  37.   
  38. Node *create()  
  39. {  
  40.     Node *p, *head;  
  41.     int n;  
  42.     head = (Node *)malloc(sizeof(Node));  
  43.     head->next = NULL;  
  44.     scanf("%d", &n);  
  45.       
  46.     while(n > 0) {  
  47.         p = (Node *)malloc(sizeof(Node));  
  48.         p->num = n;  
  49.         p->next = head->next;  
  50.         head->next = p;  
  51.         scanf("%d", &n);  
  52.     }  
  53.       
  54.     return head;  
  55. }  
  56.   
  57. Node *insert(Node *head, int pos, int num)  
  58. {  
  59.     Node *p, *pre;  
  60.     int i=1;  
  61.     pre = head;  
  62.     while(pre->next && i < pos)  
  63.     {  
  64.         pre = pre->next;  
  65.         i++;  
  66.     }  
  67.       
  68.     p = (Node *)malloc(sizeof(Node));  
  69.     p->num = num;  
  70.     p->next = pre->next;  
  71.     pre->next = p;  
  72.       
  73.     return head;  
  74. }  
  75.   
  76. Node *delete(Node *head, int num)  
  77. {  
  78.     Node *p, *pre;  
  79.     p = head->next;  
  80.       
  81.     while(p->num != num) {  
  82.         pre = p;  
  83.         p = p->next;  
  84.     }  
  85.       
  86.     if (p->num == num) {  
  87.         pre->next = p->next;  
  88.         free(p);  
  89.     }  
  90.       
  91.     return head;  
  92. }  
  93.   
  94. 6. 说明如下两片代码的区别  
  95. char *p = "love linux";  
  96. char p[] = "love linux";  
  97. 答:*p中的p是一个指针,指向一片只读的字符串  
  98. p[] 是一个数组,该数组被初始化  
  99.   
  100. 7.用C语言实现一相n!函数(要求用递归实现)  
  101. long fact(int n)  
  102. {  
  103.     if (n==0 || n==1) {  
  104.         return 1;  
  105.     }  
  106.     if(n > 1) {  
  107.         return n*fact(n-1);  
  108.     }  
  109. }  
  110.   
  111. 8. char c;  
  112.     char b[20] = "I love Linux";  
  113.     c = 'I'与C=“I” 有什么区别,字符串b在内存占几个字节  
  114. 答:c = 'I'是一个字符,c = "I" 是一个字符串,从对数组b的定义  
  115. 可知,b在内存中占有20个字节(与它进行初始化的字符串长度无关)  
  116. 9. 实现自己的mystrcat()函数  
  117. char *mystrcat(char *dst, const char *src)  
  118. {  
  119.     char *cp;  
  120.     cp = dst;  
  121.     while (*cp)  
  122.         cp ++;//指针指向字符串尾  
  123.     while(*cp++ = *src++);//每次循环最后一次拷贝了"\0"  
  124.     return dst;  
  125. }  
  126.   
  127. 10. char str[20];  
  128.     scanf("%s", str);  
  129.     printf("%s", str);  
  130. 如果输入I love linux回车,结果输出什么?为什么?  
  131. 答:输出I,因为scanf输入字符串不能有空格  
  132. 11. 已知两个整型数组均为升序排列,将两个数组合并,且合并后仍按升序排序  
  133.   
  134. //m为数组a的长度,n为数组b的长度,c是指向新开辟的数组  
  135. void combin(int *a, int *b, int *c, int m, int n)  
  136. {  
  137.     int *p1, *p2, *p3;  
  138.     for(p1 = a, p2 = b, p3 = c; p1 < a+m && p2 < b+n;)  
  139.     {  
  140.         if (*p1 < *p2)  
  141.             *p3++ = *p1++;  
  142.         else  
  143.             *p3++ = *p2++;  
  144.     }     
  145.       
  146.     while(p1 < a+m) *p3++ = *p1++;  
  147.     while(p2 < b+n) *p3++ = *p2++;  
  148.       
  149. }  
  150.       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值