gpt4 book ai didi

c - 双指针c内存故障

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

基本上,我需要编写一个拆分函数,目前我需要知道如何使用指向 s 中的字符的指针来填充子字符串

我有:

char *s = "--ab--c--";
char **substrings;
int split(char *s, int start, char sep, char **substrings, int max)

我不知道 *s**substrings 的定义是否正确。

我需要将指针分配给*s,例如**substrings将包含:

{ "", "ab", "c", "" }

子字符串的正式定义

substrings - the array to populate with pointers to substrings of s

我不知道,我用谷歌搜索了双点但无法弄清楚。

*s的大小未知,**substrings的数量只有在程序执行时才知道。

我是 C 新手,但我想我需要这样的东西:

substrings[0][0] = "";
substrings[1][0] = "a";
substrings[1][1] = "c";
substrings[2][0] = "c";
substrings[3][0] = "a";

最佳答案

尚不清楚您的 split() 例程的语义是什么,但我猜测您的 substrings 应该是一个指针数组 :

#define MAX_TOKENS  16 /* for example */
char* substrings[MAX_TOKENS];

split(s, ..., substrings, MAX_TOKENS);

关于c - 双指针c内存故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985862/

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