+ 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

19th Jun 2020, 11:36 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
8 Answers
+ 8
I tried this logic but its was giving errors
19th Jun 2020, 12:40 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 8
Gen2oo thnks
19th Jun 2020, 12:54 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 7
ThanksArsenic
19th Jun 2020, 11:52 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 6
Arsenic i also increase size but its not working
19th Jun 2020, 11:50 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
(*Javaria*) you are using an character array to store strings. Your program is allocating memory for 10 characters (not words).
19th Jun 2020, 11:45 AM
Arsenic
Arsenic - avatar
+ 3
(*Javaria*) you would need an 2D array to store strings. Here is the fix👇 https://code.sololearn.com/cYgv1Xcp4ZQ7/?ref=app
19th Jun 2020, 11:52 AM
Arsenic
Arsenic - avatar
+ 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.
19th Jun 2020, 12:34 PM
Gen2oo
Gen2oo - avatar
+ 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].
19th Jun 2020, 12:46 PM
Gen2oo
Gen2oo - avatar