int i;constchar *arr[] = {"Pippo", "Pluto", "Paperino", "Zio_Paperone"};constchar…
- User
int i;constchar *arr[] = {"Pippo", "Pluto", "Paperino", "Zio_Paperone"};constchar *(*ptr)[4] = &arr; cout << ++(*ptr)[3] << endl; linea 1 cout << ++(**ptr) << endl; linea 2 cout << (++(**ptr))[6] << endl; linea 3
Can you explain me me each row?
- Assistant
Sure, let's go through each line one by one:
-
int i: This creates an integer variable namedi. -
const char *arr[] = {"Pippo", "Pluto", "Paperino", "Zio_Paperone"};: This declares an array namedarrof typeconst char *. The array contains four elements: "Pippo", "Pluto", "Paperino", and "Zio_Paperone". -
const char *(*ptr)[4] = &arr;: This declares a pointer namedptrthat points to an array of typeconst char *with four elements. The pointer is initialized with the address of thearrarray. -
cout << ++(*ptr)[3] << endl;: This line increments the value at index 3 of the array pointed to byptrand then outputs the value to the console. In this case, it increments the value "Zio_Paperone" to "Zjp_Paperone" and outputs it.
-
- User
int main () { const char *arr[] = {"piramide", "egitto", "nave_msc", "telefono", "computer"}; const char *(*point)[5] = &arr; cout << "linea 1: " << ((*point)[1]) + 2 << endl; cout << "linea 2: " << ++(*point)[3] << endl; cout << (++(**point))[9] <<endl; cout << (*point)[3];
return 0;}
Can you send me similar exercise?
- Assistant
Certainly! Here's a modified version of the code with similar exercise:
#include <iostream> int main() { const char *arr[] = { "football", "basketball", "baseball", "tennis", "volleyball" }; const char *(*point)[5] = &arr; std::cout << "line 1: " << ((*point)[2]) + 3 << std::endl; std::cout << "line 2: " << ++(*point)[4] << std::endl; std::cout << (++(**point))[7] << std::endl; std::cout << (*point)[1]; return 0; }This code declares an array
arrof fiveconst char*elements. It also declares a pointerpointto an array ofconst char*with five elements, and initializes it with the address of thearrarray.The program then performs similar operations as before:
line 1increments the value at index 2 of the array pointed to bypointby 3 (in this case, "baseball" becomes "eball") and outputs it.