Reputation:
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:
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:
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)
Upvotes: 0
Views: 111
Reputation: 595
I don't really agree with @harper 's answer because the main problem is the following, as the OP almost figured out himself:
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..
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.
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.
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 < counter; j++)
words[position][j] = word[j];
words[position][counter] = '\0';
printf("%s%d ", words[position], position);
position++;
counter = 0;
word[0] = '\0';
}
Upvotes: 1
Reputation: 13690
Replace the last lines with this snippet:
position = 0;
while(position < 5) {
printf("%s%d ", words[position], position);
position++;
}
Upvotes: 1