mardi 24 avril 2018

What does the "temp = *(blocks + 2);" assignment means?

We are doing C++ now at my school and we recently started learning about pointers in the line "temp = *(blocks + 2);" to the temp pointer variable, we are assigning a list of characters added by a 2? It makes no sense to me so I went to the website cpp to look for information about the + operator and found out that you can concatenate strings together or characters with strings but nothing over integers.

int main()
    {
       char blocks[3] = {'A','B','C'};
       char *ptr = &blocks[0];
       char temp;

       temp = blocks[0];
       temp = *(blocks + 2);
       temp = *(ptr + 1);
       temp = *ptr;

       ptr = blocks + 1;
       temp = *ptr;
       temp = *(ptr + 1);

       ptr = blocks;
       temp = *++ptr;
       temp = ++*ptr;
       temp = *ptr++;
       temp = *ptr;

       return 0;
   }

Aucun commentaire:

Enregistrer un commentaire