本文介绍了char * arr [n]与char ** arr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打破一个更长的字符串(使用strtok())并将较小的

字符串存储在一个数组中。由于小字符串的数量不是固定/已知的b $ b,我不能使用像char * format [n]这样的声明,所以我

声明char ** format = NULL ;


现在问题开始了。每次strtok()返回一个令牌(这是
char *),我调用malloc()和我(想)存储返回的地址

数组格式化[],并将令牌复制到该地址。


代码片段:

--------------- ---

char ** format = NULL;

char delim [] =" .- /:" ;;

int i如果((tok = strtok(argv [1],delim))!= NULL)
b if((format [i] =(char *)malloc(strlen(tok)))!= NULL)

strcpy(format + i,tok);

}


for(i = 1; tok; i ++)

{

if((tok = strtok(NULL,delim) ))!= NULL)

if((format [i] =(char *)malloc(strlen(tok)))!= NULL)

strcpy(格式+ i,tok);

}


malloc语句给出了段错误。在desparation中,我在malloc中尝试了各种lvalue组合,但是没有一个是正确的

(要么我得到一个像invalid lvalue这样的编译器警告,或者赋值

从指针等等产生一个整数。或者如果编译成功,

运行总是失败并出现段错误)


问:上述方法根本不正确吗?我可以声明一些东西

作为指向char&的指针像一串字符串一样对待?如果

是的,上面的代码有什么问题?我毫不犹豫地承认

我的指针基本面还不是很清楚 - :)

~yogesh

I need to break a longer string (with strtok()) and store the smaller
strings in an array. since the number of small strings is not
fixed/known, I cannot use a declaration like char *format[n], so I
declared char** format = NULL;

Now the problem starts. Each time strtok() returns me a token (which is
char*), I call malloc() and I (want to) store the address returned in
the array format[], and copy the token to that address.

code snippet:
------------------
char **format = NULL;
char delim[] = ".-/:";
int i=0;

if ((tok = strtok(argv[1], delim)) != NULL)
{
if((format[i]=(char*)malloc(strlen(tok))) != NULL)
strcpy(format+i, tok);
}

for (i=1; tok; i++)
{
if ((tok = strtok(NULL, delim)) != NULL)
if((format[i]=(char*)malloc(strlen(tok))) != NULL)
strcpy(format+i, tok);
}

The malloc statement gives a segfault. In desparation, I have tried
various combinations of lvalue in malloc, but none of them were correct
(either I get a compiler warning like "invalid lvalue", or "assignment
makes an integer from pointer" etc.. or if compilation is successful,
run always fails with a segfault)

Q: Is above approach fundamentally incorrect? Can I declare something
as a pointer to pointer to char & treat like an array of strings? If
yes, what''s wrong in above code? I have no hesitation in admitting that
my pointer fundamentals are not very clear yet -:)
~yogesh

推荐答案



为简单起见,我们假设格式是二维数组,即

String的数组,其中字符串本身是char数组。

你正在分配格式数组的内存@ ith元素。 (第1行,

以上)

你正在复制字符串@ format + i --- format [0] + i,即1号

位置。


我不太确定。但这可能是可能的问题。

For sake of simplicity if we assume format is 2-d array, i.e. array of
String, where string itself is array of char.
you are allocating memory @ ith element of format array. (line 1,
above)
you are copying the string @ format+i ---format[0]+i , i.e. on 1st
location.

I am not much more sure. but this may be the likely catch.



Raxit


Raxit




这本身就有它的陷阱;例如,你知道strtok()

拆除它所使用的字符串,不是吗?

This in itself has its snares; for example, you do know that strtok()
demolishes the string it works on, don''t you?



这一行有两个问题。首先,没有必要抛出malloc(),而实际上这样做可以隐藏忘记#include

< stdlib.h>的错误,其中导致未定义的行为。

其次,你想为字符串_including_分配内存

终止空字符。

所以这一行应该是:


if((format [i] = malloc(strlen(tok)+1))!= NULL)


稍后同上。

然而,主要的问题是你确实为

个别字符串分配了内存,但没有为包含所有
$的数组(格式)分配内存。 b $ b指针本身。所以,虽然你会复制好的字符串,但是你写的所有内容都会变成无处不在。


这个常见问题解答可能会有所帮助:< http:// c-faq.com/aryptr/dynmuldimary.html>,

虽然在你的情况下,你事先并不知道你有多少串你的b $ b b所以你'' d也必须在格式上使用realloc()。


Richard

Two problems in this line. First, there is no need to cast malloc(), and
in fact doing so can hide the error of forgetting to #include
<stdlib.h>, which causes undefined behaviour.
Second, you want to allocate memory for the string _including_ its
terminating null character.
So this line should be:

if ((format[i]=malloc(strlen(tok)+1)) != NULL)

Ditto later on.
The major problem, however, is that you do allocate memory for the
individual strings, but not for the array (format) that contains all the
pointers themselves. So while would you copy the strings OK, everything
you write _to_ format itself goes to limbo.

This FAQ might help: <http://c-faq.com/aryptr/dynmuldimary.html>,
although in your case you don''t know in advance how many strings you
have so you''d have to use realloc() on format as well.

Richard




这本身就有它的陷阱;例如,你知道strtok()

拆除它所使用的字符串,不是吗?


This in itself has its snares; for example, you do know that strtok()
demolishes the string it works on, don''t you?



你抓住了我 - :)(但我不打算再次使用argv [1]所以它应该是

不是一个问题)

you caught me- :) (but I''m not going to use argv[1] again so it should
not be a problem)



这个问题有两个线。首先,没有必要抛出malloc(),而实际上这样做可以隐藏忘记#include

< stdlib.h>的错误,其中导致未定义的行为。


Two problems in this line. First, there is no need to cast malloc(), and
in fact doing so can hide the error of forgetting to #include
<stdlib.h>, which causes undefined behaviour.



我不明白上述观点。

I did not understand the above point.



我粘贴已更改的代码


if((tok = strtok(argv [1],delim))!= NULL)

{

format =( char **)malloc(1 * sizeof(char *));

if((format [i] =(char *)malloc(strlen(tok)+1))!= NULL)

strcpy((char *)format + i,tok);

}

/ *

* gdb显示第一个令牌复制在格式的第0位。

* /

for(i = 1; tok; i ++)

{

if((tok = strtok(NULL,delim))!= NULL)

if(format =(char **)realloc(format,(i + 1)* sizeof( char *))

!= NULL)

if((format [i] =(c har *)malloc(strlen(tok)+1))!= NULL)

strcpy((char *)format + i,tok);

}


realloc()更改格式的地址(类似于0x1)和

后续malloc()无法提供段错误。还在挠我的头!

代码中有什么问题?

~yogesh

I''m pasting the changed code

if ((tok = strtok(argv[1], delim)) != NULL)
{
format = (char**)malloc(1*sizeof(char*));
if((format[i]=(char*)malloc(strlen(tok)+1)) != NULL)
strcpy((char*)format+i, tok);
}
/*
* gdb shows the first token copied at 0th location of format .
*/
for (i=1; tok; i++)
{
if ((tok = strtok(NULL, delim)) != NULL)
if (format = (char**)realloc(format, (i+1)*sizeof(char*))
!= NULL)
if((format[i]=(char*)malloc(strlen(tok)+1)) != NULL)
strcpy((char*)format+i, tok);
}

realloc() changes the address of format (something like 0x1) and
subsequent malloc() fails giving a segfault. Still scratching my head!
What''s wrong in the code?
~yogesh


这篇关于char * arr [n]与char ** arr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 09:30