关于一个C语言的笔试题(数组+指针)

今天有个朋友和我交流了一个C语言的笔试题,感觉印象比较深,故分享下~

 

void test(void)

{

     char arr[][5];

     char *str;

     int i;

 

      str++ = arr;

      i = str - arr;

 

      //printf("%d",i);

}

 

问:i的值是多少?

 

这个题目看是很简单:就是str++后str移动的长度~

由于str指向的是char,故移动一个字节~

 

但是这个看似简单的题目却暗藏杀机,呵呵~

首先:

      定义数组时,数组的长度必须是固定的,要是没有完全显示的指出,

      则必须有初始化值,编译器会自动算出来缺失的那一项的长度。

     

其次:(str++ = arr;)这个表达式也有问题,因为str++并不是一个左值

 

感悟:这两个问题点给那个兄弟说了后,他恍然大悟,这些知识点都知道,

         也并不是多么深奥的东西,但是在就是在面试没有反应过来~

        

         做程序员做的久了,关注的东西也由小到大,由知识点到程序流程,

         由流程到程序架构,由架构到系统,旁边还要挂个协议,呵呵~确实够乱的~

         反而把自己搞的不懂得如何表达了~明明知道的东西到了关键时刻就是表达不出来~

         自己也是深有体会~

 

         另外也劝了兄弟(也是在提醒自己),面试时一定要淡定,否则回来就得蛋疼~

1. 请编写一个函数,将一个数组转化成链表,并返回链表的头指针。 ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; Node* array_to_linkedlist(int arr[], int size) { Node *head = NULL, *tail = NULL; for (int i = 0; i < size; i++) { Node *node = (Node*)malloc(sizeof(Node)); node->data = arr[i]; node->next = NULL; if (head == NULL) { head = tail = node; } else { tail->next = node; tail = node; } } return head; } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); Node *head = array_to_linkedlist(arr, size); // 遍历链表 Node *p = head; while (p != NULL) { printf("%d ", p->data); p = p->next; } return 0; } ``` 2. 请编写一个函数,判断一个链表是否是循环链表,如果是循环链表,返回 1,否则返回 0。 ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; int is_circular_linkedlist(Node *head) { if (head == NULL) { return 0; } Node *p = head, *q = head; while (q != NULL && q->next != NULL) { p = p->next; q = q->next->next; if (p == q) { return 1; } } return 0; } int main() { Node *head = (Node*)malloc(sizeof(Node)); head->data = 1; Node *p1 = (Node*)malloc(sizeof(Node)); p1->data = 2; head->next = p1; Node *p2 = (Node*)malloc(sizeof(Node)); p2->data = 3; p1->next = p2; Node *p3 = (Node*)malloc(sizeof(Node)); p3->data = 4; p2->next = p3; // 将链表变成循环链表 p3->next = head; printf("%d\n", is_circular_linkedlist(head)); return 0; } ``` 3. 请编写一个函数,实现队列的入队和出队操作。 ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct Queue { int data[MAX_SIZE]; int front, rear; } Queue; Queue* create_queue() { Queue *q = (Queue*)malloc(sizeof(Queue)); q->front = q->rear = 0; return q; } int is_full(Queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } int is_empty(Queue *q) { return q->front == q->rear; } void enqueue(Queue *q, int x) { if (is_full(q)) { printf("Queue is full.\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAX_SIZE; } int dequeue(Queue *q) { if (is_empty(q)) { printf("Queue is empty.\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAX_SIZE; return x; } int main() { Queue *q = create_queue(); enqueue(q, 1); enqueue(q, 2); enqueue(q, 3); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值