+ 1

Usage of * in a char array

int a; char *arr[] = {"c", "c++", "java", "vba"}; char *(*ptr)[4] = &arr; cout << ++(*ptr)[2]; I am comparing line 3 of the above code with int*p = &var. Is it correct to say that the first * in line 3 is of the same idea with the one seen in int*p = &var? What, then, is the purpose of the second * seen in line 3? Is it part of the pointer variable's name? In the 4th line, how come the * outside of the parenthesis is not included? And finally, does the ++ operator print the "a" or "ava" of (*ptr)[2]? Why?

29th Jul 2020, 1:13 AM
Solus
Solus - avatar
1 Answer
+ 4
Solus let's see what is happening at each and every line of code. here in line 2 you have declared a 2-D array of strings which is nothing but an array of pointers pointing at starting of each string. In line 3 you create another array of pointers and that is pointing to starting of your arr[]. Line 4 you are accessing the pointer that is pointing to the third element( (*ptr)[2] ) of array (which is again a pointer pointing to letter 'j' in "java") And as soon as you increment this one(using ++ operator) , it starts pointing at 'a' of "java" Thus the output is "ava" Hope it is much clear now but if you are still confused then do let me know, I will try to simplify it a bit further.🙂👍
29th Jul 2020, 4:27 AM
Arsenic
Arsenic - avatar