鱼C论坛

 找回密码
 立即注册
查看: 1518|回复: 11

[已解决]求解

[复制链接]
发表于 2022-11-20 13:29:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学到现在,还是不知道指针有什么用?
最佳答案
2022-11-20 13:40:32
下面是一个list类型的通用容器,这个程序使用了指针
指针没有用吗?如果不用指针的话,要怎么实现这样的程序呢?

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>

  6. void output(const list_t *list, size_t index) {
  7.     if(index >= list_size(list)) return;
  8.     output(list, index + 1);
  9.     int temp; list_get(list, index, &temp, sizeof(temp));
  10.     printf("%d ", temp);
  11. }

  12. int main(void) {
  13.     list_t *list = list_init();
  14.     for(size_t i = 0; i < 5; ++i) {
  15.         int temp; scanf("%d", &temp);
  16.         list_append(list, &temp, sizeof(temp));
  17.     }
  18.     output(list, 0); puts("");
  19.     list_deinit(list);
  20.     return 0;
  21. }
  22. sh-5.1$ cat list.h
  23. #ifndef _LIST_H_
  24. #define _LIST_H_

  25. #include <stddef.h>
  26. #include <stdbool.h>

  27. struct list_node_tag {
  28.     void *data; size_t size;
  29.     struct list_node_tag *next;
  30. };

  31. typedef struct {
  32.     struct list_node_tag *head;
  33.     size_t size;
  34. } list_t;

  35. list_t *list_init(void);
  36. void list_deinit(list_t *list);
  37. bool list_clean(list_t *list);
  38. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  39. bool list_delete(list_t *list, size_t index);
  40. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  41. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  42. bool list_append(list_t *list, const void *data, size_t size);
  43. size_t list_size(const list_t *list);
  44. bool list_empty(const list_t *list);

  45. #endif
  46. sh-5.1$ cat list.c
  47. #include "list.h"
  48. #include <stdlib.h>
  49. #include <memory.h>

  50. list_t *list_init(void) {
  51.     list_t *list = malloc(sizeof(*list));
  52.     if(!list) return NULL;
  53.     list->head = NULL;
  54.     list->size = 0;
  55.     return list;
  56. }

  57. void list_deinit(list_t *list) {
  58.     if(!list) return;
  59.     list_clean(list);
  60.     free(list);
  61. }

  62. bool list_clean(list_t *list) {
  63.     if(!list) return false;
  64.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  65.     return true;
  66. }

  67. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  68.     if(!list) return false;
  69.     if(list_size(list) < index) return false;
  70.     if(!data) return false;
  71.     struct list_node_tag **current = &list->head;
  72.     while(index--) current = &(*current)->next;
  73.     struct list_node_tag *node = malloc(sizeof(*node));
  74.     if(!node) return false;
  75.     node->data = malloc(size);
  76.     if(!node->data) {free(node); return false;}
  77.     memcpy(node->data, data, size);
  78.     node->size = size;
  79.     node->next = *current;
  80.     *current = node;
  81.     ++list->size;
  82.     return true;
  83. }

  84. bool list_delete(list_t *list, size_t index) {
  85.     if(!list) return false;
  86.     if(list_size(list) <= index) return false;
  87.     struct list_node_tag **current = &list->head;
  88.     while(index--) current = &(*current)->next;
  89.     struct list_node_tag *temp = *current;
  90.     *current = temp->next;
  91.     free(temp->data); free(temp);
  92.     --list->size;
  93.     return true;
  94. }

  95. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  96.     if(!list) return false;
  97.     if(list_size(list) <= index) return false;
  98.     if(!data) return false;
  99.     struct list_node_tag *const *current = &list->head;
  100.     while(index--) current = &(*current)->next;
  101.     struct list_node_tag *temp = *current;
  102.     if(temp->size > size) return false;
  103.     memcpy(data, temp->data, temp->size);
  104.     return true;
  105. }

  106. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  107.     bool res = list_delete(list, index);
  108.     return res ? list_insert(list, index, data, size) : res;
  109. }

  110. bool list_append(list_t *list, const void *data, size_t size) {
  111.     if(!list) return false;
  112.     return list_insert(list, list_size(list), data, size);
  113. }

  114. size_t list_size(const list_t *list) {
  115.     if(!list) return 0;
  116.     return list->size;
  117. }

  118. bool list_empty(const list_t *list) {
  119.     if(!list) return true;
  120.     return list_size(list) == 0;
  121. }
  122. sh-5.1$ gcc -g -Wall -o main main.c list.c
  123. sh-5.1$ ./main
  124. 1 2 3 4 5
  125. 5 4 3 2 1
  126. sh-5.1$
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-20 13:36:21 | 显示全部楼层
        那你就认为它没用好了,这有什么好纠结的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 13:40:32 | 显示全部楼层    本楼为最佳答案   
下面是一个list类型的通用容器,这个程序使用了指针
指针没有用吗?如果不用指针的话,要怎么实现这样的程序呢?

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>

  6. void output(const list_t *list, size_t index) {
  7.     if(index >= list_size(list)) return;
  8.     output(list, index + 1);
  9.     int temp; list_get(list, index, &temp, sizeof(temp));
  10.     printf("%d ", temp);
  11. }

  12. int main(void) {
  13.     list_t *list = list_init();
  14.     for(size_t i = 0; i < 5; ++i) {
  15.         int temp; scanf("%d", &temp);
  16.         list_append(list, &temp, sizeof(temp));
  17.     }
  18.     output(list, 0); puts("");
  19.     list_deinit(list);
  20.     return 0;
  21. }
  22. sh-5.1$ cat list.h
  23. #ifndef _LIST_H_
  24. #define _LIST_H_

  25. #include <stddef.h>
  26. #include <stdbool.h>

  27. struct list_node_tag {
  28.     void *data; size_t size;
  29.     struct list_node_tag *next;
  30. };

  31. typedef struct {
  32.     struct list_node_tag *head;
  33.     size_t size;
  34. } list_t;

  35. list_t *list_init(void);
  36. void list_deinit(list_t *list);
  37. bool list_clean(list_t *list);
  38. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  39. bool list_delete(list_t *list, size_t index);
  40. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  41. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  42. bool list_append(list_t *list, const void *data, size_t size);
  43. size_t list_size(const list_t *list);
  44. bool list_empty(const list_t *list);

  45. #endif
  46. sh-5.1$ cat list.c
  47. #include "list.h"
  48. #include <stdlib.h>
  49. #include <memory.h>

  50. list_t *list_init(void) {
  51.     list_t *list = malloc(sizeof(*list));
  52.     if(!list) return NULL;
  53.     list->head = NULL;
  54.     list->size = 0;
  55.     return list;
  56. }

  57. void list_deinit(list_t *list) {
  58.     if(!list) return;
  59.     list_clean(list);
  60.     free(list);
  61. }

  62. bool list_clean(list_t *list) {
  63.     if(!list) return false;
  64.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  65.     return true;
  66. }

  67. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  68.     if(!list) return false;
  69.     if(list_size(list) < index) return false;
  70.     if(!data) return false;
  71.     struct list_node_tag **current = &list->head;
  72.     while(index--) current = &(*current)->next;
  73.     struct list_node_tag *node = malloc(sizeof(*node));
  74.     if(!node) return false;
  75.     node->data = malloc(size);
  76.     if(!node->data) {free(node); return false;}
  77.     memcpy(node->data, data, size);
  78.     node->size = size;
  79.     node->next = *current;
  80.     *current = node;
  81.     ++list->size;
  82.     return true;
  83. }

  84. bool list_delete(list_t *list, size_t index) {
  85.     if(!list) return false;
  86.     if(list_size(list) <= index) return false;
  87.     struct list_node_tag **current = &list->head;
  88.     while(index--) current = &(*current)->next;
  89.     struct list_node_tag *temp = *current;
  90.     *current = temp->next;
  91.     free(temp->data); free(temp);
  92.     --list->size;
  93.     return true;
  94. }

  95. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  96.     if(!list) return false;
  97.     if(list_size(list) <= index) return false;
  98.     if(!data) return false;
  99.     struct list_node_tag *const *current = &list->head;
  100.     while(index--) current = &(*current)->next;
  101.     struct list_node_tag *temp = *current;
  102.     if(temp->size > size) return false;
  103.     memcpy(data, temp->data, temp->size);
  104.     return true;
  105. }

  106. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  107.     bool res = list_delete(list, index);
  108.     return res ? list_insert(list, index, data, size) : res;
  109. }

  110. bool list_append(list_t *list, const void *data, size_t size) {
  111.     if(!list) return false;
  112.     return list_insert(list, list_size(list), data, size);
  113. }

  114. size_t list_size(const list_t *list) {
  115.     if(!list) return 0;
  116.     return list->size;
  117. }

  118. bool list_empty(const list_t *list) {
  119.     if(!list) return true;
  120.     return list_size(list) == 0;
  121. }
  122. sh-5.1$ gcc -g -Wall -o main main.c list.c
  123. sh-5.1$ ./main
  124. 1 2 3 4 5
  125. 5 4 3 2 1
  126. sh-5.1$
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 13:53:19 From FishC Mobile | 显示全部楼层
指针是c的灵魂,指哪打哪,强大的令人发指
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 13:56:45 | 显示全部楼层
指针?

指针在一些数据结构里很重要

例如链表

其实不用太纠结于指针的写法,需要用到是百度一下就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 14:02:08 | 显示全部楼层
本帖最后由 jackz007 于 2022-11-20 14:09 编辑

       有一个基本事实是,所有的数组、字符串都是作为指针传递给函数的,如果没有指针,那么,有一件事可以肯定,无法把数组、字符串等内存块性质的数据传递进函数进行处理。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 14:04:45 From FishC Mobile | 显示全部楼层
xiaosi4081 发表于 2022-11-20 13:56
指针?

指针在一些数据结构里很重要

何止重要,简直就是非常非常非常重要

无论是数据还是地址还是指令代码,都得用到指针,否则cpu就无法寻址。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 14:15:05 From FishC Mobile | 显示全部楼层
要实现多态,就得用到指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 16:44:54 | 显示全部楼层
说实话指针在强大和我毛关系,就当它没用好了,纠结什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 17:12:35 | 显示全部楼层
去看小甲鱼的带你学C,指针那部分可以多刷几遍
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-20 17:26:11 | 显示全部楼层
指针就是内存地址,相当于现实中的地址
可以把内存想象成储物柜,指针就是储存东西后,储物柜的钥匙牌,上面有你储存东西的盒子的编号相当于指针,你可以根据这个编号可以找到那个被存的东西
你也可以把钥匙牌放到其他箱子,放钥匙牌的箱子的钥匙牌就是指针的指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-2 17:13:59 | 显示全部楼层
名称:C++指针详解
链接:https://blog.csdn.net/weixin_39640298/article/details/84900326
(转载在CSDN)
指针是“指向(point to)”另外一种类型的复合类型。复合类型是指基于其它类型定义的类型。

理解指针,先从内存说起:内存是一个很大的,线性的字节数组。每一个字节都是固定的大小,由8个二进制位组成。最关键的是,每一个字节都有一个唯一的编号,编号从0开始,一直到最后一个字节。

程序加载到内存中后,在程序中使用的变量、常量、函数等数据,都有自己唯一的一个编号,这个编号就是这个数据的地址。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-19 19:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表