Post fix variable to pointer example

Im not sure I understand when i can actually assign using the post-fix pointer way. Did this example for making sure im not miss anything
(last row does not work, is it work on class only example? ) .

#include <iostream>
#include <string>

///This simple cpp just demostrate pointers and refernces Barakooda, 09\11\16.

int main()
    //lets create variable int type called mooki and give it a value of 101.
    int mooki = 101;

    //lets create pointer called someone.
    int *someone;

    //someone will be pointer to the address of mooki.
    someone = &mooki;

    //lets print mooki address in two ways .
    std::cout << "Mooki Adress is " << someone << std::endl;
    std::cout << "Mooki Adress is " << &mooki << std::endl << std::endl;

    //lets print mooki value in two ways.
    std::cout << "Mooki value is " << mooki << std::endl;
    std::cout << "Mooki value is " << *someone << std::endl << std::endl;

    //lets add the refernce to mooki
    int &refernce = mooki;

    std::cout << "refernce value is " << refernce << std::endl;
    std::cout << "refernce adress is " << &refernce << std::endl << std::endl;

    //lets change refernce value (point to mooki)
    refernce = 102;

    std::cout << "refernce value is " << refernce << std::endl;
    std::cout << "Mooki value is " << mooki << std::endl << std::endl;

    //lets try and save the pointer of variable mooki (postfix).
    int *postfixExample ;
    //doesnt work
    postfixExample = mooki*;


I think the * at the end of mooki* is the problem.
Have you tried:

postfixExample = mooki;



int main(){
    int mooki = 101; //Here we declare mooki variable as an int and initialize it. 
    int *mooki_reference = &mooki;  //here we declare mooki_reference as pointer to an int, and we're initializing it with mooki memory address that we're getting using the "&" symbol. Basically, mooki_reference can be seen as "label" that gives a name to the physical memory address of mooki. 
    int dereference_mooki = *mooki_reference ; //here we're declaring an int and then initializing it with the "content" of the memory address pointed by mooki_reference, which at this point, the memory address of mooki; remember mooki_reference  is just a "label" to a memory address, so using the star before mooki_reference, we're saying: "I don't want his address, I want mooki himself!", so we are copying the content of mooki into dereference_mooki...  :) 
//Also, if at this point: dereference_mooki and mooki will have the same value. 
    return 0;

I am not sure if this is clear to you, but I think this has no mystery, it’s kind of a simple concept I think… :slight_smile:

I found out what Ben meant, all is good,
it only wanted to show how reference or pointer act on a “type” not a variable \ object.

I think I didn’t understand your question. Sorry if I looked as any of these 3 points you are referring.
Good to know you found out your answer.

Thank you for trying to help,
Appreciated .

Oh you just deleted the 3 points…
Anyways, sorry again, and remember, we are all trying to help…

Not sure if this is what you wanted to kow, but here it goes.
In this piece of code:

 //lets try and save the pointer of variable mooki (postfix).
    int *postfixExample ;
    //doesnt work
    postfixExample = mooki*;

mooki* won’t do anything but return an error.
Let me give you an example:

int arr[4] = { 10,20,30,40 }; //declaring an array of 4 elements
int *p = arr; //at this point, p will point to the first element in the array, which is 10...
p++; //here we increase "the pointer" position and p will be pointing to the second element, which is 20
int *valueOne = p++; //now here, using postfix "++" will make "valueOne" to be pointing to, also, the second element in the array, that is 20; and the reason is the operator precedence. This will load the actual address referenced by "p" into "valueOne"  and  "then" increase the pointed address in "p".
int *valueTwo = ++p; //now here, using a prefix it will do the opposite as the previous line, "valueTwo" here will point to the fourth element in the array, 40, because the operator precedence will increment the pointer in "p" first, and then return it. 

Have a nice day!

One of the things that I didn’t like about this video is the mention of postfix unary * and &, since those don’t actually exist. Unary * and & (as opposed to binary * (i.e. multiplication) and & (i.e. bitwise and)) are always prefix operators, even when used in declarations where variable names aren’t used.

This is why when declaring multiple pointers, you have to type * for each one.

int *a, *b;

One of the negative design decisions of C is using an expression based declaration syntax, which means, that declarations look like expressions, only there is a type name before the expression. Theoretically, this causes declarations and expressions to be consistent in terms of syntax, but it also seems to make the language harder to use and learn.

So, while int * is a type, the * is a prefix operator of a conceptual variable name rather than a postfix of int.

Privacy & Terms