使用strsep()与C字符串的动态数组 [英] Using strsep() with dynamic array of strings in C

查看:150
本文介绍了使用strsep()与C字符串的动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

#include <string.h>

int main(void) {
    char *buffer = NULL, **words = NULL, *aPtr = NULL, *sPtr;
    int count = 0;

    buffer = strdup("The quick brown fox jumps over the lazy dog");
    sPtr = buffer;

    do {
    	aPtr = strsep(&sPtr, " ");

    	words[count++] = ... // missing code
    } while(aPtr);

    return 0;
}

我错过了一些code,你可以在上面看到...是否有任何的strdup()的,在这个情况下工作?该的strdup()函数本身似乎并没有工作...如果没有一个,我如何才能让这片code的工作?

I'm missing some code as you can see above... Is there any kind of strdup() that works on this situation? The strdup() function itself doesn't seem to work... If there isn't one, how can I make this piece of code work?

指针的指针是头疼,我...

Pointer of pointer is headache for me...

推荐答案

到目前为止还没有分配字[0] 字[1 ] ,......因此,使用的strdup 于事无补。

As yet you have not allocated words[0], words[1], ... so using strdup doesn't help.

更糟的是,你不知道提前多少字有将要,所以它是不平凡的的malloc 所需的空间。

Worse, you don't know in advance how many words there are going to be, so it is not trivial to malloc the space you need.

一种选择是,以取代用链表或动态数组。

One option is to replace words with a linked list, or a dynamic array.

这篇关于使用strsep()与C字符串的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆