+ 13
Need a help to fix this code...?
I want If i enter books then it traverse in that char *p array..if found output available otherwise not https://code.sololearn.com/csajBBnFj0D2/?ref=app
8 Answers
+ 8
I tried this logic but its was giving errors
+ 8
Gen2oo thnks
+ 7
ThanksArsenic
+ 6
Arsenic i also increase size but its not working
+ 3
(*Javaria*) you are using an character array to store strings. Your program is allocating memory for 10 characters (not words).
+ 3
(*Javaria*) you would need an 2D array to store strings.
Here is the fixđ
https://code.sololearn.com/cYgv1Xcp4ZQ7/?ref=app
+ 2
I see no problem with using an array of pointers here? At least if you're not storing anything into it or plan to modify the strings.
const char *p[] = {
"books",
"pencils",
"pens",
"notebook",
"eraser"
};
The input is stored in the 'str' array which is then compared to p[i]. C++ is only warning you for not using const for the array, because it contains pointers to read-only strings.
+ 2
(*Javaria*)
Here's an example: https://code.sololearn.com/cKku39glhbzc/#cpp
C++ is strict about const and non-const conversions. I believe in C you would not get those warnings if you declared them as char *, but for safety reasons they should be const.
I believe the other reason was that you did strcmp(str, *p[i]) instead of p[i].