字符串和指针数组 [英] arrays of strings and pointers

查看:59
本文介绍了字符串和指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:


char buf [10] [10000];


printf("%lp \ n",buf);

printf("%lp \ n",& buf [0]);

printf("%lp \ n" ;,buf [0]);

printf("%lp \ nn",buf [1]);

printf("%d \ n" ;,buf [1] -buf [0]);


前3个printfs给出相同的结果,最后2个显示

buf [1距离buf [0] 10000。这是预期的结果吗?


我原以为结果会是:


buf:


这应该是指向一组10个指针中的第一个的指针。这些

指针将由buf [0] ... buf [9]访问。他们会指向$ 10000 $ b 10 10000个char字符串(这是一块内存)。


& buf [0]


这应该与上面相同。 buf [0]与*(buf + 0)相同,所以

& buf [0]是&(*(buf + 0)),它应该只是buf。


buf [0]


这应该指向第一个字符串。它不应该和

& buf [0]相同。


buf [1]


这是预期的10000比buf [0]高。


我有一个函数,它的输入是一个指针数组:


void funct(char ** base);


我原以为我可以把buf传给它,是否有必要为
创建10手动数组指针?但是,它看起来像buf

是指向char的类型指针而不是指向char的指针。

I have the following code:

char buf[10][10000];

printf("%lp\n", buf);
printf("%lp\n", &buf[0]);
printf("%lp\n", buf[0]);
printf("%lp\n", buf[1]);
printf("%d\n", buf[1]-buf[0]);

The first 3 printfs give the same result and the last 2 show that
buf[1] is 10000 away from buf[0]. Is this the expected result?

I had assumed that the result would have been:

buf:

This should be a pointer to the first of a set of 10 pointers. These
pointers would be accessed by buf[0] ... buf[9]. They would point to
the 10 10000 char strings (which are a single block of memory).

&buf[0]

This should be the same as above. buf[0] is the same as *(buf+0), so
&buf[0] is &(*(buf+0)) which should be just buf.

buf[0]

This should point to the first string. It shouldn''t be the same as
&buf[0].

buf[1]

This is as expected 10000 higher than buf[0].

I have a function that takes as its input an array of pointers:

void funct(char **base);

I had assumed that I could just pass buf to it, is it necessary to
create the 10 array of pointers manually ? However, it looks like buf
is of type pointer to a char rather than pointer to a pointer to a char.

推荐答案

2005年10月26日09:10:23 -0700,在comp.lang.c中, ra ***** @ netscape.net

写道:
On 26 Oct 2005 09:10:23 -0700, in comp.lang.c , ra*****@netscape.net
wrote:
buf [0]

这应该指向第一个字符串。它不应该与
& buf [0]相同。


它是。考虑一下。

void funct(char ** base);

我原以为我可以把buf传递给它,是否有必要创建
手动10个指针数组?


不,这个都行。你/实际/想做什么?停止尝试

猜测发生了什么!

但是,看起来buf
是指向char的类型指针而不是指向char的指针。
buf[0]

This should point to the first string. It shouldn''t be the same as
&buf[0].
It is. Think about it.
void funct(char **base);

I had assumed that I could just pass buf to it, is it necessary to
create the 10 array of pointers manually ?
No, this''ll work. What are you /actually/ trying to do? Stop trying to
guess whats happening!
However, it looks like buf
is of type pointer to a char rather than pointer to a pointer to a char.




如果它定义如上,它是一个字符数组数组。


注意,这不是与指针或指向

指针的指针相同。


-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.ungerhu.com/ jxh / clc.welcome.txt>


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全使用网新闻== ----
< a rel =nofollowhref =http://www.newsfeeds.comtarget =_ blank> http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----



If its defined as above, its an array of arrays of chars.

Note that this isn''t the same as either a pointer or a pointer to a
pointer.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----




Mark McIntyre写道:

Mark McIntyre wrote:
但是,看起来buf
是指向char的类型指针,而不是指向指向的指针一个字符。
However, it looks like buf
is of type pointer to a char rather than pointer to a pointer to a char.


如果它的定义如上,它是一个字符数组数组。

请注意,这与指针不同或指向
指针的指针。



If its defined as above, its an array of arrays of chars.

Note that this isn''t the same as either a pointer or a pointer to a
pointer.




一旦内存已经不是数组和指针基本相同

分配等?区别在于你不能移动到

数组库的位置,而且数组也会自动分配内存

。例如,这些基本上是等价的(假设您忽略了运行时和编译时内存分配差异的
):


char buf [255];


strcpy(buf," string");


printf("%c",buf [3]);





char * buf;


buf = malloc(sizeof(char)* 7);


strcpy(buf," string");


printf("%c",buf [3]);


你也可以在这两种情况下使用* buf,它会给你第一个

字符的刺痛。


无论如何,当你开始使用数组数组处理

时,也许这种等价性会崩溃。


所以,我想问题是,如果我将buf定义为:


char buf [10] [10000];


并希望将它传递给函数,函数原型应该是什么? >
是吗?我假设一个双数组实际上是一个指向

指针的指针。


另外,我认为它仍然通过引用传递并且不会将

整个数组复制到函数的局部变量。



Isn''t an array and a pointer basically the same once memory has been
allocated etc. ? The difference being that you can''t move where the
array base is and also that the array will have memory allocated
automatically. For example, these are basically equivalent (assuming
you ignore run-time and compile time memory allocation differences):

char buf[255];

strcpy(buf,"string");

printf("%c", buf[3]);

and

char *buf;

buf = malloc(sizeof(char)*7);

strcpy(buf,"string");

printf("%c", buf[3]);

You can also use *buf in both cases and that will give you the first
character of the sting.

Anyway, perhaps this equivalence breaks down when you start dealing
with arrays of arrays.

So, I guess the question is, if I define buf as:

char buf[10][10000];

and want to pass it to a function, what should the function prototype
be ? I had assumed that a double array was effectively a pointer to a
pointer.

Also, I assume that it still passes by reference and doesn''t copy the
entire array to the function''s local variables.


ra ***** @ netscape.net 写道:
无论如何,当你开始用数组处理时,这种等价可能会破裂数组。

所以,我想问题是,如果我将buf定义为:

char buf [10] [10000];

并希望将它传递给一个函数,函数原型应该是什么?我假设一个双数组实际上是指向指针的指针。

另外,我认为它仍然通过引用传递并且不会复制
整个数组函数的局部变量。
Anyway, perhaps this equivalence breaks down when you start dealing
with arrays of arrays.

So, I guess the question is, if I define buf as:

char buf[10][10000];

and want to pass it to a function, what should the function prototype
be ? I had assumed that a double array was effectively a pointer to a
pointer.

Also, I assume that it still passes by reference and doesn''t copy the
entire array to the function''s local variables.




是的,这是你感到困惑的地方。一个乘法维数的数组是

仍然只是一个指针,而不是一个指针指针。

char buf [10] [10000]分配相同的内存char buf [10 * 10000]

类似于buf =(char *)malloc(10 * 10000 * sizof(char));


区别在于,如果你做char buf [10] [10000],编译器就足够聪明了,知道如何增加内存地址,这取决于两个索引。

。 br />

所以你可以拥有


char buf [10] [10000];


printf( "%c",buf [i] [j])


相当于


char buf [10 * 10000]

printf("%c",buf [j * 10 + i])//我想我说对了 - 大师:请

检查我这个/>

所以将多维数组的数组传递给函数是一件痛苦的事。这里'

你是怎么做的


char buf [10] [10000]

dosomething(buf);


无效dosomething(char buf [10] [])

{

buf [i] [j]

}


编译器需要知道所有尺寸的大小,除了

最后一个。因为内部编译器正在执行j * 10 + i

步骤,它只是将它隐藏起来。


有两种解决方法:


1 - 如果你真的需要将内存中的东西作为一个

矩阵(做快速数学的东西),那么你可以照顾j * 10 + i

自己动手


char buf [10 * 10000];

dosomething(buf);


无效dosomething(char * buf)

{

buf [j * 10 + i]; //与buf [i] [j]

}相同

2 - 如果你不关心速度,那么你可以做你做的事情用指针来指示




char ** buf;


buf =(char ** )malloc(10 * sizeof(char *));

for(i = 0; i< 10; i ++)

{

buf [i] =(char *)malloc(10000 * sizeof(char));

}

dosomething(buf)

for(i = 0; i< 10; i ++)

{

免费(buf [i]);

}

免费(buf);


无效dosomething(char ** buf)

{

buf [i] [j] ; //就像你预期的那样

}


大师:请随时纠正任何错误陈述,我想我知道

什么我正在谈论,但是当我读到这个新闻组时,我意识到

很多ci都不知道。


mike



yes, this is where you''re confused. a multiply dimentioned array is
still just a pointer, not a pointer to a pointer.

char buf[10][10000] allocates the same memory as char buf[10*10000]
which is similar to buf=(char*)malloc(10*10000*sizof(char));

the difference is that if you do char buf[10][10000] the compiler is
smart enough to know how to increment the memory addresses depending on
the two indices.

so you could have

char buf[10][10000];

printf("%c",buf[i][j])

which is equivalent to

char buf[10*10000]
printf("%c",buf[j*10+i]) //i think i got that right - gurus: please
check me on this

so passing a multiply dimensioned array to a function is a pain. here''s
how you do it

char buf[10][10000]
dosomething(buf);

void dosomething(char buf[10][])
{
buf[i][j]
}

the compiler needs to know the size of all the dimensions except for
the last one. becasue internally the compiler is doing the j*10+i
step, it''s just hiding it from you.

there are two ways around this:

1 - if you actually need to have the stuff in memory ordered as a
matrix (doing fast math stuff), then you can take care of the j*10+i
step yourself

char buf[10*10000];
dosomething(buf);

void dosomething(char* buf)
{
buf[j*10+i]; //same as buf[i][j]
}

2 - if you don''t care about speed, then you can do what you were
thinking of with pointers to pointers

char** buf;

buf=(char**)malloc(10*sizeof(char*));
for (i=0;i<10;i++)
{
buf[i]=(char*)malloc(10000*sizeof(char));
}
dosomething(buf)
for (i=0;i<10;i++)
{
free(buf[i]);
}
free(buf);

void dosomething(char** buf)
{
buf[i][j]; //just as you expected
}

gurus: please feel free to correct any misstatements, i think i know
what i''m talking about, but when i read this newsgroup i realize how
much c i don''t know.

mike


这篇关于字符串和指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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