作者:好人森森_195 | 来源:互联网 | 2023-05-18 16:37
WhatIamtryingtodo,isparsestring,containingspacestoarrayofarraysofchars.Soifinput
What I am trying to do, is parse string, containing spaces to array of arrays of chars. So if input was "ab cd ef", I want my array to be array[0]==ab, array[1]==cd,array[2]==ef. I hope that that makes it clear. Problem however is that i cant use any lib functions. No string.h, no fget, just scanf and printf. Here is snipplet of my code:
我正在尝试做的是解析字符串,包含字符数组数组的空格。因此,如果输入是“ab cd ef”,我希望我的数组是数组[0] == ab,数组[1] == cd,数组[2] == ef。我希望这说清楚。但问题是我无法使用任何lib函数。没有string.h,没有fget,只有scanf和printf。这是我的代码的snipplet:
char word[25] = "";
char *words[25];
for (int i = 0; i <= find_length(input); i++){
if(input[i] == ' ' || i == find_length(input)){
words[position] = word;
printf("%s%d ",words[position], position);
position++;
counter = 0;
word[0] = '\0';
}
else{
word[counter] = input[i];
counter++;
}
}
position = 0;
while(counter <5){
printf("%s%d ",words[position], position);
counter++;
}
First I parse string into individual words and then I try to put them into array. I hope that my logic is correct. So the problem is, that first printf (one in for loop) prints proper values and their positions. However second printf (which is only to make sure that string was parsed properly) prints just 0. So for example input "ab cd ef" gives:
首先,我将字符串解析为单个单词,然后我尝试将它们放入数组中。我希望我的逻辑是正确的。所以问题是,第一个printf(for循环中的一个)打印正确的值及其位置。然而,第二个printf(仅用于确保正确解析字符串)仅打印0.因此,例如输入“ab cd ef”给出:
ab0 cd1 ef2 0 0 0 0 0
ab0 cd1 ef2 0 0 0 0 0
I suspect that problem is that I am not adding values to the array of arrays, just assigning pointers. And since I dump "word", they point to nothing. If that is correct, how do I assign values instead of pointers?
我怀疑问题是我没有为数组数组添加值,只是分配指针。因为我倾倒“字”,他们指向什么都没有。如果这是正确的,我如何分配值而不是指针?
Of course this assumption might be wrong, if so could you point me to my mistake and how to fix it? Thank you.
当然这个假设可能是错误的,如果是这样,你能否指出我的错误以及如何解决它?谢谢。
PS: I know that I should strcpy value of string not = it, but as I said, I cant use any lib functions other than scanf and printf (find_length is my own function)
PS:我知道我应该strcpy string的值not = it,但正如我所说,我不能使用除scanf和printf之外的任何lib函数(find_length是我自己的函数)
2 个解决方案
1
I don't really agree with @harper 's answer because the main problem is the following, as the OP almost figured out himself:
我真的不同意@harper的回答,因为主要的问题如下,因为OP几乎想出了自己:
I suspect that problem is that I am not adding values to the array of arrays, just assigning pointers. And since I dump "word", they point to nothing. If that is correct, how do I assign values instead of pointers?
我怀疑问题是我没有为数组数组添加值,只是分配指针。因为我倾倒“字”,他们指向什么都没有。如果这是正确的,我如何分配值而不是指针?
This code will make words[0]
point to the block of memory associated with the char array word
. Then words[1]
will point to that same block. Then words[3]
and so on..
此代码将使单词[0]指向与char数组字相关联的内存块。然后单词[1]将指向同一个块。然后单词[3]等等..
In the end, all elements inside the char* array words[]
will point to the char array word[]
, which will contain a '\0'
as its first element, seeing that the OP does this word[0] = '\0';
repeatedly.
最后,char * array words []中的所有元素都将指向char数组word [],它将包含一个'\ 0'作为其第一个元素,看到OP执行此单词[0] ='\ 0' ;反复。
So my proposed fix, besides what's already mentioned about the while loop at end, would be to allocate memory for each substring and then copy the contents of word[]
with a simple for
loop.
所以我提出的修复,除了已经提到的关于while循环的内容之外,将为每个子字符串分配内存,然后用简单的for循环复制word []的内容。
So this:
所以这:
for (int i = 0; i <= find_length(input); i++){
if(input[i] == ' ' || i == find_length(input)){
words[position] = word;
printf("%s%d ",words[position], position);
position++;
counter = 0;
word[0] = '\0';
}
Would become this:
会变成这样的:
for (int i = 0; i <= find_length(input); i++) {
if(input[i] == ' ' || i == find_length(input)) {
words[position] = malloc(sizeof(char) * (counter + 1));
for (int j = 0; j