0% found this document useful (0 votes)
10 views16 pages

Programming Fundamentals 12

Pointers in C++ are variables that store the memory addresses of other variables. Pointers allow programs to simulate call-by-reference and create and manipulate dynamic data structures. Pointers help simplify program complexity and improve execution speed.

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views16 pages

Programming Fundamentals 12

Pointers in C++ are variables that store the memory addresses of other variables. Pointers allow programs to simulate call-by-reference and create and manipulate dynamic data structures. Pointers help simplify program complexity and improve execution speed.

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 16

COMPUTER

PROGRAMMING
TOPIC: POINTERS
POINTERS

• In C++, pointers are variables that store the memory addresses of other variables.
ADDRESS IN C++

• If we have a variable var in our program, &var will give us its address in the memory. For
example,
• Here, 0x at the beginning represents the address is in the hexadecimal form.

• Notice that the first address differs from the second by 4 bytes and the second address
differs from the third by 4 bytes.

• This is because the size of an int variable is 4 bytes


C++ POINTERS

• As mentioned above, pointers are used to store addresses rather than values.

• Here is how we can declare pointers.

• int *pointVar;

• Note: The * operator is used after the data type to declare pointers.
ASSIGNING ADDRESSES TO POINTERS

• int* pointVar, var;


Here, 5 is assigned to the variable var. And, the
• var = 5; address of var is assigned to the pointVar
pointer with the code pointVar = &var.

• // assign address of var to pointVar pointer


• pointVar = &var;
GET THE VALUE FROM THE ADDRESS USING
POINTERS
• int* pointVar, var;
Here, 5 is assigned to the variable var. And, the
• var = 5;
address of var is assigned to the pointVar
pointer with the code pointVar = &var.
• // assign address of var to pointVar
• pointVar = &var;

• // access value pointed by pointVar


• cout << *pointVar << endl; // Output: 5
• cout << pointVar << endl; // Output: 0x7ffefc62d97c
WORKING OF C++ POINTERS
CHANGING VALUE POINTED BY POINTERS

If pointVar points to the address of var, we can change the value of var by using *pointVar.
For example
int var = 5;
int* pointVar;
// assign address of var
pointVar = &var;
// change value at address pointVar
*pointVar = 1;
cout << var << endl; // Output: 1
//Here, pointVar and &var have the same address, the value of var will also be changed when *pointVar is changed.
1. Import the iostream header file. This will allow us to use the functions defined in the header file without getting errors.

2. Include the std namespace to use its classes without calling it.

3. Call the main() function. The program logic should be added within the body of this function. The { marks the beginning
of the function’s body.

4. Declare an integer variable x and assigning it a value of 27.

5. Declare a pointer variable *ip.

6. Store the address of variable x in the pointer variable.

7. Print some text on the console.

8. Print the value of variable x on the screen.

9. Print some text on the console.

10. Print the address of variable x. The value of the address was stored in the variable ip.

11. Print some text on the console.

12. Print value of stored at the address of the pointer.

13. The program should return value upon successful execution.

14. End of the body of the main() function.


NULL POINTER

1. Declare a pointer variable ip and assigning it a


value of NULL.
2. Print value of pointer variable ip alongside
some text on the console.
3. The program must return value upon successful
completion.
4. End of the body of the main() function.
EXAMPLE USING ARRAY
COMMON MISTAKES WHEN WORKING WITH
POINTERS
ADVANTAGES OF USING POINTERS

1. Pointers are variables which store the address of other variables in C++.
2. More than one variable can be modified and returned by function using pointers.
3. Memory can be dynamically allocated and de-allocated using pointers.
4. Pointers help in simplifying the complexity of the program.
5. The execution speed of a program improves by using pointers.
SUMMARY

1. A pointer refers to a variable holding address of another variable.


2. Each pointer has a valid data type.
3. A pointer is a symbolic representation of a memory address.
4. Pointers allow programs to simulate call-by-reference and create and manipulate dynamic data structures.
5. Arrays and pointers use a related concept.
6. The array name denotes the array’s base.
7. If you want to assign the address of an array to a pointer, don’t use an ampersand (&).
8. If there is no specific address to assign a pointer variable, assign it a NULL.

You might also like