+ 1

Error "error: invalid types ‘char[int]’ for array subscript" in C++.

Hello, I have this code that gives me the error from the title: This is problem statement: Given a string of "s" consisting of two words separated by space (first name and name), write the two words formatted so that the first word (first name) starts with a capital letter and the rest of the characters are written in normal letters and the second word (name) should be written in capital letters only Input: s "mAriN Ion" And the expected output must be : s2 "Marin ION" #include <iostream> #include <cstring> #include <string.h> using namespace std; int main() { char s[50], s2[50]; cin.getline(s, 50); char *pp = strtok(s, " "); while (pp != NULL){ pp = strtok(NULL, " "); } tolower(pp[1]); pp[1][1] = char(int(pp[1][1]) - 32); toupper(pp[2]); strcat(s2, pp[1]); strcat(s2, pp[2]); cout << s << "\n" << s2; return 0; } https://code.sololearn.com/c1KqiUbbqlEc/#cpp Someone can explain me how to solve this error. Thank you very much.

12th May 2020, 9:17 AM
Joita Vladut
Joita Vladut - avatar
1 ответ
+ 1
Joita Vladut , most important thing to observe is pp[1][1]. Being pp as char* , it is having single dimension array of char.... So at max you should be doing to index pp is level one... It means pp[x] , where x is index zero to max length minus 1, is allowed... pp[][] is double index and it is not allowed on char*... It could have been allowed if pp would have been char** i.e. double pointer.
12th May 2020, 6:37 PM
Ketan Lalcheta
Ketan Lalcheta - avatar