TUTORIAL! Creating a Simple Program with Pointer Concepts.

in #utopian-io7 years ago (edited)

What Will I Learn?

  • Learn the Pointer concept in C ++
  • Learn simple programming with Pointer concept

Requirements

  • Dev C++ 5.11

Difficulty

  • Intermediate

Tutorial Contents

Pointer (hint) is a variable that contains the address of another variable. This pointer is used with the intention to point to a memory address so that we can easily identify the address of a variable. Pointer can also be interpreted as a data type whose value leads to the value contained in the memory address.

The main Pointer function is to store the memory address of a variable. Pointers can also serve to store a memory address of a function.

Definition:
A pointer is a variable of a certain type that can be used to record the address of a variable that is similar to it, accessing and manipulating data stored in the address. If int x is a variable of type integer then & x means address of x. If p is a pointer of type integer then p can store and hold the address of the x.

Kinds - Kind of Pointer:

  • Operator Dereference (&)

Dereference Operator (&) is a functioning operator declaring a variable in memory replacement. This operator is usually called the "address of". If you want to know where the variable will be stored, we can provide an ampersand (&) in front of the variable, which means "address of".

From the example of the above program, the value of a becomes 0096F7DC, by adding "&" to the variable length, this is what the dereference operator means, ie converting a variable value into a memory address.

  • Operator Reference (*)

This Reference operator is opposite to the Dereference operator where the value of the variable becomes the memory address whereas the reference operator changes the memory address to the variable value.

The Reference operator (*) is an operator that can directly access the values ​​contained in a pointered variable, this can be done by adding an asterisk identifier. This operator is usually called "value pointed by"

From the above example, the value of * a becomes the number 20, by adding "*" to variable a, where the memory address 0096F7DC returns to a variable value of 20, this is the reference operator, ie converting a memory address back into a variable value.

Example of Pointer C ++ program:

The code of program

#include <iostream>

using namespace std;

int main()
{
     //variable x declaration with int data type
     int x;

     //pointer declaration p with int data type
     int *p;

     x = 20;
     p = &x;

     cout<<"\t\t===== Example of a Program Pointer =====\n\n";

     cout<<"Value X   \t\t: "<<x<<endl;
     cout<<"Addres X (&X) \t\t: "<<&x<<endl<<endl;

     cout<<"Designated Value *P\t: "<<*p<<endl;
     cout<<"Designated Address *P\t: "<<p<<endl;
     cout<<"Address *P (&P) \t\t: "<<&p<<endl;

     // fill in the value 30 into * p / address pointed pointer p
     // to assign values ​​via pointer address use *
     // because the designated memory address of the pointer p is the variable memory address x
     // then the value of the variable x also changes
     *p = 30;

     cout<<"\n\n\nValue X   \t\t: "<<x<<endl;
     cout<<"Address X (&X) \t\t: "<<&x<<endl<<endl;

     cout<<"Designated Value *P\t: "<<*p<<endl;
     cout<<"Designated Addres *P\t: "<<p<<endl;
     cout<<"Address *P (&P) \t\t: "<<&p<<endl;

     return 0;
}

PointerII.PNG

PoibterIII.PNG

Result Work:

Pointer.PNG

In the above program we initially create a variable with an int data type with the name "x", then a pointer with int data type with the name "p". after that give the value of x with value "20" then direct / fill / show pointer p to memory address variable x. after that displays data from variable x and pointer p,

Then code * p = 30; means that we will fill the value 30 into the pointer p / point designated pointer p, because the memory address designated ponter p is the memory address variable x then the value of the variable x also change, To prove the value of the variable x also change (to 30) then in the code below it we will display data from variable x and pointer p. How do you understand?

Articles about Creating a Simple Program with Pointer Concepts., hopefully this article can be useful for my friend either to add knowledge, to do task, and to just add insight about Pointer on C ++ and Mind Operator Pointer both Operator Dereference (&) and Operator Reference (*).

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • There is no proper repository for C/C++ contributions, so no chance of approvals for those.

You can contact us on Discord.
[utopian-moderator]

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by syakirrahmatilla from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.029
BTC 68542.93
ETH 2454.71
USDT 1.00
SBD 2.54