gpt4 book ai didi

c - 在 C 中添加一个指向数组(指针)开头的指针

转载 作者:行者123 更新时间:2023-11-30 15:50:19 24 4
gpt4 key购买 nike

我有一个字符指针数组

char ** strings

长度为

limit

我试图将一个 char 指针添加到数组的开头,同时维护除最后一个之外的所有数组对象例如,如果我的数组有 3 个指针:

 {*str1, *str2, *str3}

我想在开头添加*str4,它看起来像这样:

 {*str4, *str1, *str2}

同时保持相同的大小

希望我说得足够清楚

谢谢

编辑

我试图避免循环整个过程来移动指针。我正在为此寻找 O(1) 解决方案

最佳答案

可以用链表概念来完成。[首先插入]

步骤:

1)元素声明。

 struct element{
char *dataPtr;
struct element *next;
};

2)main中的头元素声明。

struct element *head;

3)将 head 传递给插入函数

struct element *new =(struct element *)malloc(sizeof(element));
insert(&head,new)

4)在插入函数中,

if (*head == NULL)
{
*head = new;
new ->Next = *head;
}
else
{
new->Next = *head;
*head = new;
}

在这一步中,您不需要遍历整个链表。

关于c - 在 C 中添加一个指向数组(指针)开头的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15835681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com