如何将任何长度的字符串存储到char *类型的数组中? [英] how to store strings of any length into an array of type char*?

查看:83
本文介绍了如何将任何长度的字符串存储到char *类型的数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的帖子标题为:

"如何将任意长度的字符串输入到类型的数组中:char

* array [SIZE]?"

似乎造成了混乱。因此我解释了我的问题

以下。


考虑以下程序:

#include< stdio.h>

#define SIZE 1

int main()

{

char * array [SIZE];

scanf("%s",array [0]); //输入任意长度的字符串。

for(int i = 0; *(array [0] + i)!=''\'''; i ++)

printf("%c",*(array [0] + i));

返回0;

}

当你运行这个程序时,你会发现printf通过scanf输出

整个字符串,无论你的

字符串有多长。


现在假设您将常量SIZE更改为更大的值4,对于

示例,然后将程序修改为:


#include< stdio.h> ;

#define SIZE 4


int main()

{

char * array [ SIZE] = {" string of any size"," type",

" praetertranssubstantiationali * stically"," another string"};

for(int i = 0; i< SIZE; i ++){

for(int j = 0; *(array [i] + j)!=''\'''; j ++){

printf("%c",*(array [i] + j));

}

printf(" \\\
" ;);

}

返回0;

}


然后这个程序将输出数组已完全初始化的字符串。


但如果你想要字符串b e由用户在运行时输入,而不是如上所述在初始化时给出,那么你是如何做到这一点的?

即在执行过程中输入你的字符串一个一个,他们得到

完全按照上面的程序存储。


任何想法?

-arko

my earlier post titled:
"How to input strings of any lengths into arrays of type: char
*array[SIZE] ?"
seems to have created a confusion. therefore i paraphrase my problem
below.

consider the following program:
#include<stdio.h>
#define SIZE 1
int main()
{
char *array[SIZE];
scanf("%s", array[0]); // type a string of any length whatsoever.
for(int i = 0; *(array[0] + i) != ''\0''; i++)
printf("%c", *(array[0] + i));
return 0;
}

when you run this program, you will find that the "printf" outputs the
whole string which you entered through "scanf", no matter how long your
string was.

now suppose you change the constant SIZE to some bigger value, 4, for
example, and then modify the program to this:

#include<stdio.h>
#define SIZE 4

int main()
{
char *array[SIZE] = {"string of any size", "type",
"praetertranssubstantiationali*stically", "another string"};
for(int i = 0; i < SIZE; i++){
for(int j = 0; *(array[i] + j) != ''\0''; j++){
printf("%c", *(array[i] + j));
}
printf("\n");
}
return 0;
}

then this program will output the strings with which the array has been
initialized exactly.

but if you want that the strings be entered at run time by user, rather
than be given at initialization as above, then how do you do this?
that is, during execution you type your strings one by one and they get
stored exactly as in the above program.

any ideas?
-arko

推荐答案

文章< 11 ********** ***********@g14g2000cwa.googlegroups。 com>,

< ar ****** @ gmail.com>写道:
In article <11*********************@g14g2000cwa.googlegroups. com>,
<ar******@gmail.com> wrote:
考虑以下程序:
#include< stdio.h>
#define SIZE 1
int main()
{
char * array [SIZE];
scanf("%s",array [0]); //输入任意长度的字符串。
for(int i = 3D 0; *(array [0] + i)!= 3D''\ 0''; i ++)
printf( "%c",*(array [0] + i));
返回0;
}
运行此程序时,您会发现printf输出你通过scanf输入的整个字符串,无论你的字符串有多长。
consider the following program:
#include<stdio.h>
#define SIZE 1
int main()
{
char *array[SIZE];
scanf("%s", array[0]); // type a string of any length whatsoever.
for(int i =3D 0; *(array[0] + i) !=3D ''\0''; i++)
printf("%c", *(array[0] + i));
return 0;
} when you run this program, you will find that the "printf" outputs the
whole string which you entered through "scanf", no matter how long your
string was.




它不会甚至可以在我的C89编译器上编译。


如果我使用gcc --std = c99编译它,那么如果我输入

甚至一个输入字符,它就会进行coredump。


Purify抱怨未初始化的内存读取。那不是很好,因为你传递的是数组[0]的内容,这是你没有初始化的数据。


当我输入输入时,Purify然后抱怨Null Pointer Write。

这是因为它已经发生 - 数组[0]的值为0

(恰好是我系统上的NULL指针),所以当你通过scanf()调用写入该缓冲区时,它会尝试写入NULL。

反过来因为一个SIGSEGV触发一个COR,coredump

(分段违规。)

我没有读过剩下的问题了,因为它是基于你的程序正常运行的真实性,

它绝对没有。

-

熵是概率的对数 - Boltzmann



It doesn''t even compile on my C89 compiler.

If I compile it with gcc --std=c99 then it coredumps if I enter
even one character of input.

Purify complains about an uninitialized memory read. That''s not
surprising, as you are passing in the -content- of array[0], which
you have not initialized.

When I put in input, Purify then complains about Null Pointer Write.
That''s because it -happened- that array[0] had the value 0
(which happens to be the NULL pointer on my system), so when you
write to that buffer via the scanf() call, it tries to write to NULL.
That in turn triggers a COR, coredump because of a SIGSEGV
(Segmentation Violation.)
I didn''t bothere to read the rest of your question, as it was
predicated upon the truth of your program functioning properly,
which it absolutely does not do.
--
Entropy is the logarithm of probability -- Boltzmann


亲爱的沃尔特,

我发布的程序运行绝对正确我的

Turboc ++ IDE编译器。

如果你不介意给我你的电子邮件ID,那么我可以给你发送

源代码和.exe文件。你可以自己看看。

-arko

dear walter,
the programs which i have posted run absolutely correctly on my
Turboc++ IDE compiler.
if you don''t mind giving me your email id, then i can send you both the
source code and the .exe file. you can see for yourself.
-arko


文章< 11 ********** ***********@g43g2000cwa.googlegroups。 com>,

< ar ****** @ gmail.com>写道:
In article <11*********************@g43g2000cwa.googlegroups. com>,
<ar******@gmail.com> wrote:
亲爱的沃尔特,
我发布的程序在我的
Turboc ++ IDE编译器上运行得非常正确。


*叹气。*

如果你不介意给我你的电子邮件ID,那么我可以发给你两个
源代码和.exe文件。你可以亲眼看看。
dear walter,
the programs which i have posted run absolutely correctly on my
Turboc++ IDE compiler.
*Sigh.*
if you don''t mind giving me your email id, then i can send you both the
source code and the .exe file. you can see for yourself.




我不这么认为。我已经解释了代码的问题,

并且被另一张海报回应。


重新阅读scanf()的文档。在格式化后传入的值是什么?

?地址,对吧?而你

正在传递数组[0]的内容作为地址,对吗?

现在,那是什么内容 - ?你初始化了数组[0]吗?

如果没有,那么你怎么知道它的价值是多少?在C标准中是否有任何规则来控制价值是多少?b $ b变量的自动变量是什么?存储如果你没有明确地说

初始化它们?如果是这样,那么什么是默认值,并且

是什么导致你认为它将恰好是一块无限长的存储块的位置?
?如果

没有自动变量的默认值,那么是什么导致你

认为碰巧在那里的随机值将是

一大块存储空间的位置是无限长的吗?

-

如果你喜欢VT-52那就太费了。



I don''t think so. I already explained the problems with the code,
and was echoed by another poster.

Re-read the documentation for scanf(). What are the values
that you pass in after the format? Addresses, right? And you
are passing the -content- of array[0] as the address, right?
Now, what is that -content-? Did you initialize array[0] ?
If not, then how do you know what the value will be? Is there
any rule in the C standard that controls what the value is
of variables of "automatic" storage if you do not explicitly
initialize them? If so, then what is the default value, and
what leads you to think that it will happen to be the location
of a chunk of storage which is indefinitely long? If there is
no default value for automatic variables, then what leads you
to think that the random value that happens to be there will be
the location of a chunk of storage which is indefinitely long?
--
Feep if you love VT-52''s.


这篇关于如何将任何长度的字符串存储到char *类型的数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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