• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Re-Learning C++

Codex NG

Recurrent Flamer
Joined
Jul 24, 2015
Messages
2,994
Solutions
12
Reaction score
1,657
Since I have some extra time on my hands and would love to learn how to edit the sources I am reading & trying the examples on this site, (my c++ is terrible).
http://www.tutorialspoint.com/cplusplus/

So i am messing around with basic stuff like pointers and addresses, trying to increment them & their values.
Besides isn't this what programming is all about?

Trial and error..

Not exactly what I wanted
Code:
#include <iostream>
using namespace std;

int main()
{
    // integer array a with 3 elements
   int a[3] = {2, 2, 3};
   // integer pointer b which is pointing to a's address
   int *b = &a[0];
   // same thing as saying a[0] = a[0] + 1 
   *b = *b + 1;
   // shows the results
   cout << *b;
}

Still not what I wanted
Code:
#include <iostream>
using namespace std;

int main()
{
   int a[3] = {1, 2, 5};
   int *b = &a[0];
   // same as above but instead we use the size of *b which is 4 bytes
   for(int i = 0; i < (sizeof(*b) - 1); i++){
        // increment the pointer to point to a different address of a
       cout << *b++ <<endl;
   }
   
}

Almost there...
Code:
#include <iostream>
using namespace std;

int main()
{

   int a[3] = {1, 2, 5};
   int *b = &a[0];
   // this won't work as expected, it skips the initial index, post or pre it is always off by 1
   while(*++b){

       cout << *b <<endl;
   }
   
}


Ahhh, now we are getting somewhere :)
Code:
#include <iostream>
using namespace std;

int main()
{
   int a[3] = {1, 2, 5};
   int *b = &a[0];
   // this is what i wanted to do :D
   do{
       cout << *b <<endl;
   }while(*++b);
   
}
 
Back
Top