字符串数组,malloc [英] Arrays of Strings, malloc

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

问题描述



我正在尝试读取文件中的数据列表。每一行在第一列中都有一个字符串在

中。这就是我想要阅读的内容。我可以先说

char [1000] []读取1000行数组(我认为!!)。


但是我想要使用malloc。每个字符串最多50个字符,并且

可能有0到数千行。我如何实际启动数组?

我见过char **数组等。起初我尝试了char * array [50]但我认为

可以提供50个指针to chars :(。一旦我知道有多少行,我如何使用malloc为

数组设置空间?是吧

array = malloc(lines * sizeof) (char *))??


最后,我想用printf打印每个字符串。如何访问

说第30行?


我用双打,英镑等成功完成了几次,但是现在

带字符串似乎是一个''二维'问题而我'与它的

指针方面相混淆。


任何人都可以通过简单的解决方案启动我吗?


谢谢

Ian


---

邮件已通过无病毒验证。

由AVG反病毒系统( http://www.grisoft.com)检查

版本:6.0.556 /病毒库:348 - 发布日期:26/12/2003

Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a ''two dimesional'' problem and I''m confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?

Thanks
Ian

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003

推荐答案

Ian Todd写道:
Ian Todd wrote:
一旦我知道有多少行,我如何使用malloc为
数组设置空间?是不是数组= malloc(行* sizeof(char *))??


是的。


我不知道你怎么知道有多少行。

你读过两次文件吗?如果是这样,我建议改为从

malloc()开始,例如一个包含50个字符串的数组,然后当你在

文件中读取时,realloc()只要你用完了当前分配的空间,就可以使用前一个大小的两倍。当你到达文件末尾时,

realloc()将其缩小到必要的大小,以释放你分配的未使用的

空间。

最后,我想使用printf打印每个字符串。我如何访问
说第30行?


array [29]。假设当你说第30个时,你从1开始计算线数。

我用双打,英镑等成功完成了几次,但是现在用字符串来看似乎是两分钱问题,我对它的
指针方面感到困惑。
How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??
Yes.

I don''t know how you know beforehand how many lines there are, though.
Do you read the file twice? If so, I''d suggest to instead start by
malloc()ing e.g. an array of 50 strings, and then as you read in the
file, realloc() it to twice the previous size whenever you have used up
the currently allocated space. When you reach the end of the file,
realloc() it down to just the necessary size, to free the unused
space you allocated.
Finally, I want to print each of the strings using printf. How do I access
say the 30th line?
array[29]. Assuming you count lines from 1 when you say 30th.
I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a ''two dimesional'' problem and I''m confused with the
pointer aspect of it.




它是一样的。把它想象成'char *'的一维数组,

因为字符串是char *。


但是char *字符串需要malloced空间本身,所以这部分有点比b或b更复杂:当你读到一个字符串时,你会

必须要malloc它的空间(使用malloc(字符串长度+ 1)),然后

读取或复制字符串到该空间。之后,当你完成后,

你必须释放()每个字符串,然后你释放()字符串数组。


如果你不是知道字符串可以有多长,你可以使用realloc

技巧如上所示:malloc一个通常足够的大小,读取字符串

a一段时间,根据需要重新分配,并在完成后将字符串重新分配到

实际大小。或者您可能只有一个输入缓冲区

,您可以这样处理,并在读取它们时将其中的字符串复制到新的

分配的字符串中。 />

-

Hallvard



It''s just the same. Think of it as a one-dimensional array of ''char*''s,
since a string is a char*.

But a char* string needs malloced space itself, so that part is a bit
more complicated than int or double: W hen you read in a string, you''ll
have to malloc space for it (with malloc(length of string + 1)), then
read or copy the string into that space. Later, when you are finished,
you must free() each string before you free() the array of strings.

If you don''t know how long the strings can be, you can use the realloc
trick as above: malloc a size which is often enough, read in the string
a piece of a time, realloc as needed, and realloc the string down to its
actual size when done. Or you might just have a single input buffer
which you treat that way, and copy the strings from it into newly
allocated strings when you have read them in.

--
Hallvard


Ian Todd写道:
Ian Todd wrote:


我正在尝试从文件中读取数据列表。
每行在第一列中都有一个字符串。这就是我想要阅读的内容。我可以先说
char [1000] []读取数组的1000行(我想!!)。

但是我想使用malloc。
每个字符串最多50个字符,并且可能有零到几千行。
我如何实际启动数组?
我看过char **数组等。起初我试过char * array [50]
但是我认为它给了50个字符指针:(
。一旦我知道有多少行,我怎么用malloc为
数组设置空间?是
array = malloc(lines * sizeof(char *))??

最后,我想用printf打印每个字符串。
我如何访问说第30行?

我用双打,
整数等成功完成了几次,但是现在用字符串来看似乎是一个''二维'问题<我很困惑它的指针方面。

任何人都可以通过简单的解决方案启动我吗?

Hi,
I am trying to read in a list of data from a file.
Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc.
Each string is at most 50 characters long, and
there may be zero to thousands of lines.
How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50]
but I think that gives 50 pointers to chars :(
.How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf.
How do I access say the 30th line?

I have sucessfully done this a few times with doubles,
ints etc, but now
with strings it seems a ''two dimesional'' problem
and I''m confused with the pointer aspect of it.

Can anyone start me off on a simple solution?




/ * BEGIN hello.c * /


#inclu de< stdio.h>

#include< stdlib.h>

#include< string.h>


#define LINES 1000

#define LENGTH 50

int main(无效)

{

char(* line)[LENGTH],(*指针)[50];


line = malloc(LINES * sizeof * line);

if (!行){

fputs(" malloc failure \ n",stderr);

退出(EXIT_FAILURE);

}

strcpy(第[30]行,你好,);

strcpy(第[31]行,世界);

strcpy(line [32],"");

for(pointer = line + 30; **指针; ++指针){

fputs(*指针,标准输出);

}

putchar(''\ n'');

免费(线);

返回0;

}


/ * END hello.c * /


-

pete



/* BEGIN hello.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINES 1000
#define LENGTH 50

int main(void)
{
char (*line)[LENGTH], (*pointer)[50];

line = malloc(LINES * sizeof *line);
if (!line) {
fputs("malloc failure\n", stderr);
exit(EXIT_FAILURE);
}
strcpy(line[30], "hello, ");
strcpy(line[31], "world");
strcpy(line[32], "");
for (pointer = line + 30; **pointer; ++pointer) {
fputs(*pointer, stdout);
}
putchar(''\n'');
free(line);
return 0;
}

/* END hello.c */

--
pete


pete写道:
I / Todd写道:

Ian Todd wrote:

您好,
我正在尝试从文件中读取数据列表。
每行都有一个字符串在第一栏中。这就是我想要阅读的内容。我可以先说
char [1000] []读取数组的1000行(我想!!)。

但是我想使用malloc。
每个字符串最多50个字符,并且可能有零到几千行。
我如何实际启动数组?
我看过char **数组等。起初我试过char * array [50]
但是我认为它给了50个字符指针:(
。一旦我知道有多少行,我怎么用malloc为
数组设置空间?是
array = malloc(lines * sizeof(char *))??

最后,我想用printf打印每个字符串。
我如何访问说第30行?

我用双打,
整数等成功完成了几次,但是现在用字符串来看似乎是一个''二维'问题<我很困惑它的指针方面。

任何人都可以通过简单的解决方案启动我吗?

Hi,
I am trying to read in a list of data from a file.
Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc.
Each string is at most 50 characters long, and
there may be zero to thousands of lines.
How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50]
but I think that gives 50 pointers to chars :(
.How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf.
How do I access say the 30th line?

I have sucessfully done this a few times with doubles,
ints etc, but now
with strings it seems a ''two dimesional'' problem
and I''m confused with the pointer aspect of it.

Can anyone start me off on a simple solution?



/ * BEGIN你好.c * /

#include< stdio .h>
#include< stdlib.h>
#include< string.h>

#define LINES 1000
#define LENGTH 50
int main(void)
{char(* line)[LENGTH],(*指针)[50];



/* BEGIN hello.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINES 1000
#define LENGTH 50

int main(void)
{
char (*line)[LENGTH], (*pointer)[50];




(*指针)[LENGTH]


-

pete



(*pointer)[LENGTH]

--
pete


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

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