如何在C中返回字符串数组(char **) [英] How to Return an array of strings in C (char**)

查看:87
本文介绍了如何在C中返回字符串数组(char **)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C中实现Split函数。

如果我有一个字符串:char * S ="这是一个测试;

和如果我尝试基于空格''''进行拆分,它应该返回一个

字符串数组,如:


{" This,is is ;,a,test}。


我试图按照下面的说明实现它,但我得到了一个分段

错误。如果有人能在这个问题上给我答案,我真的很感激




char ** split(char str [],int * c) {

int start = 0;

int end = 0;

char ** temp = NULL;

char ** ptr;

int count = 0;

int length = strlen(str);

//首先计算空格数

//知道数组中元素的数量

while(str){

if(* str =='''' )count ++;

}

printf(" count value is%d \ n",count);

//我怀疑关于malloc的问题

ptr =(char **)malloc(sizeof(char)*(count + 1));

* c = count + 1;


while(结束<长度){

ptr = str + start;

if(str [end]!='' ''|| length){

end ++;

}

if(str [end]!=''\''') {//用\替换空格

str [end] =''\ 0'';

}

if(!温度计p = ptr;

ptr ++;

}

返回临时数;

}

谢谢

解决方案

< vi ******* @ gmail.comwrote:


>我正在尝试在C中实现Split函数。

如果我有一个字符串:char * S ="这是一个测试 ;;

如果我尝试基于空格''''进行拆分,它应该返回一个

字符串数组,如:


{" This,is,a,test}。


我试图按照下面给出的方式实现它,但我正在进行细分

错误。如果有人能在这个问题上给我答案,我真的很感激




char ** split(char str [],int * c) {

int start = 0;

int end = 0;

char ** temp = NULL;

char ** ptr;

int count = 0;

int length = strlen(str);

//首先计算空格数

//知道数组中元素的数量

while(str){

if(* str =='''' )count ++;

}

printf(" count value is%d \ n",count);

//我怀疑关于malloc的问题

ptr =(char **)malloc(sizeof(char)*(count + 1));

* c = count + 1;


while(结束<长度){

ptr = str + start;

if(str [end]!='' ''|| length){

end ++;

}

if(str [end]!=''\''') {//用\替换空格

str [end] =''\'0' ;

}

if(!temp)temp = ptr;

ptr ++;

}

返回temp;

}



我不喜欢编码,其中malloc和free是不同的

函数除非有充分的理由这样做。所以我不会从分裂中返回

任何东西(返回无效):让调用者分配空间,告诉

分割空间,在适当位置修改并拥有打电话给谁b $ b负责免费。如果这是一个学习练习,你将需要使你想要返回静态的数组。我希望您需要继续的

信息在FAQ的链接中,如果没有发布

另一个问题。

http://c-faq.com/~scs/ cgi-bin / faqcat.cgi?sec = aryptr




< vi ******* @ gmail .comwrote in message


>我正在尝试在C中实现Split函数。

如果我有一个字符串:char * S =这是一个测试;

如果我尝试基于空格'''进行拆分,它应该返回一个

字符串数组,如:


{" This,is,a,test}。


我试图将其实现为给出以下但我得到的分段

错误。如果有人能在这个问题上给我答案,我真的很感激




char ** split(char str [],int * c) {

int start = 0;

int end = 0;

char ** temp = NULL;

char ** ptr;

int count = 0;

int length = strlen(str);



如果您剪切并粘贴此代码,这必须是您崩溃的地方。输入

必须为null或未正确设置。


>

//首先计算空格数

//知道元素数量在数组中

while(str){

if(* str =='''')count ++;

}



原因是你没有增加str。除非输入是一个空的
字符串,否则这将永远运行。由于程序没有挂起,可能是

崩溃在上面。


>

printf(" count value is%d \ n",count);

//我对malloc也很怀疑

ptr =(char **)malloc(sizeof(char)*(count + 1));



你想要的(count + 1)* sizeof(char *)。

这是一个指针数组,而不是字符数组。


>

* c = count + 1;


while(end< length ){



* ptr = str + start。


ptr = str + start;



在这种情况下长度是多少?


if(str [end]!=''''|| length){

end ++;

}



如果这肯定是其他的话。


if(str [end]!=''\'''){//用\替换空格

str [结束] = '' \0 '';



现在你需要将* ptr设置为str + start。你还需要更新start,并且

增量结束。


{



这让我很困惑。在进入循环之前设置温度要好得多。


if(!temp)temp = ptr;

ptr ++;

}


return temp;

}


谢谢



它几乎就在那里。通常,像这样调试rotuintes的方法是在诊断printfs中滑动
。例如,您可以打印输入

printf(" ***%s *** \ n",str)以查看它是否有效。星号在那里

拿起任何有趣的空间。你可以打印在这里在进入

主循环之前,看看你是否真的到了那里,然后str +开始看看

你在每次传球的字符串中去哪里。


vi ******* @ gmail.com 写道:


>

我正在尝试在C中实现Split函数。

如果我有一个字符串:char * S ="这是一个测试;

如果我尝试基于空格分开''''它应该返回

一系列字符串,如:


{" This,is,a,test}。


我试图按照下面的说明实现它,但是我得到了一个

分段错误。我真的很感激,如果有一个

可以在这个问题上给我一个答案:



搜索谷歌档案我的帖子 toksplic.c"。


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


-

发表于:来自 http://www.teranews.com 的免费Usenet帐户


I am trying to implement the Split function in C ie.
if i have a string: char* S="This is a test";
and if i try to split based on space '' '' it should return an array of
strings like:

{"This","is","a","test"} .

I tried to implement it as given below but am getting a segmentation
fault. I would really appreciate if some one could give me an answer
on this issue:

char** split(char str[],int*c) {
int start=0;
int end =0 ;
char** temp=NULL;
char** ptr;
int count=0;
int length = strlen(str);
//first counting the number of spaces
// to know the number of elements in the array
while (str) {
if (*str=='' '') count++;
}
printf("count value is %d \n",count);
// I am doubt ful about the malloc too
ptr = (char**) malloc(sizeof(char)*(count+1));
*c = count+1;

while(end<length) {
ptr = str + start;
if (str[end]!='' '' || length) {
end++;
}
if (str[end]!=''\0'') {// replacing spaces with \0
str[end]=''\0'';
}
if (!temp) temp= ptr;
ptr++;
}
return temp;
}

Thanks

解决方案

<vi*******@gmail.comwrote:

>I am trying to implement the Split function in C ie.
if i have a string: char* S="This is a test";
and if i try to split based on space '' '' it should return an array of
strings like:

{"This","is","a","test"} .

I tried to implement it as given below but am getting a segmentation
fault. I would really appreciate if some one could give me an answer
on this issue:

char** split(char str[],int*c) {
int start=0;
int end =0 ;
char** temp=NULL;
char** ptr;
int count=0;
int length = strlen(str);
//first counting the number of spaces
// to know the number of elements in the array
while (str) {
if (*str=='' '') count++;
}
printf("count value is %d \n",count);
// I am doubt ful about the malloc too
ptr = (char**) malloc(sizeof(char)*(count+1));
*c = count+1;

while(end<length) {
ptr = str + start;
if (str[end]!='' '' || length) {
end++;
}
if (str[end]!=''\0'') {// replacing spaces with \0
str[end]=''\0'';
}
if (!temp) temp= ptr;
ptr++;
}
return temp;
}

I am not a fan of coding where the malloc and free are in different
functions unless there is a good reason to do so. So I would not return
anything (return void) from split: have the caller allocate the space, tell
split where the space is, modify it in place and have the caller be
responsible for doing the free. If this is a learning exercise, you will
have to make the array you are trying to return static. I hope the
information you need to continue is in the link to the FAQ, if not post
another question.

http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr



<vi*******@gmail.comwrote in message

>I am trying to implement the Split function in C ie.
if i have a string: char* S="This is a test";
and if i try to split based on space '' '' it should return an array of
strings like:

{"This","is","a","test"} .

I tried to implement it as given below but am getting a segmentation
fault. I would really appreciate if some one could give me an answer
on this issue:

char** split(char str[],int*c) {
int start=0;
int end =0 ;
char** temp=NULL;
char** ptr;
int count=0;
int length = strlen(str);

This must be where you crashed, if you cut and pasted this code. The input
must be null or not set up correctly.

>
//first counting the number of spaces
// to know the number of elements in the array
while (str) {
if (*str=='' '') count++;
}

The reason is that you fail to increment str. Unless input is an empty
string, this will run forever. Since the program didn''t hang, probably the
crash was above.

>
printf("count value is %d \n",count);
// I am doubt ful about the malloc too
ptr = (char**) malloc(sizeof(char)*(count+1));

you want (count + 1) * sizeof(char *).
It is an array of pointers, not of chars.

>
*c = count+1;

while(end<length) {

*ptr = str + start.

ptr = str + start;

What is length doing in this condition ?

if (str[end]!='' '' || length) {
end++;
}

this should surely be an else if.

if (str[end]!=''\0'') {// replacing spaces with \0
str[end]=''\0'';

Now you need to set *ptr to str + start. You also need to update start, and
increment end.

{

This confused me. Much better to set temp before entering the loop.

if (!temp) temp= ptr;
ptr++;
}

return temp;
}

Thanks

Its nearly there. Generally the way to debug rotuintes like this is to slip
in diagnostic printfs. For instance you could have printfed your input
printf("***%s***\n", str) to see it was valid. The asterisks are there to
pick up any funny spaces. The you could have printed "here" before entering
the main loop to see if you actually got there, then str + start to see
where you go to in the string on each pass.


vi*******@gmail.com wrote:

>
I am trying to implement the Split function in C ie.
if i have a string: char* S="This is a test";
and if i try to split based on space '' '' it should return
an array of strings like:

{"This","is","a","test"} .

I tried to implement it as given below but am getting a
segmentation fault. I would really appreciate if some one
could give me an answer on this issue:

Search the google archives for my posting of "toksplic.c".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于如何在C中返回字符串数组(char **)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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