08/13/2022

Topics:

Memory addresses.
Declaration.
Dereferencing a pointer.
Pointers to pointer.

Static vs. dynamic objects:

So, new and delete are the object of Pointer.

Computer Memory:

Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there.

Pointer in C

Pointer in c :

A pointer is a variable used to store the address of a memory cell.

Than, We can use the pointer to reference this memory cell.

Pointer in C

Pointer in c Types:

Pointer:

So, C has pointer types for each type of object
1. Pointers to int objects
2. Pointers to char objects
3. Pointers to user-defined objects
(e.g., RationalNumber)
– Moreover, Even pointers to pointers
• So, Pointers to pointers to int objects

Pointer in c Variable:

Declaration of Pointer variables:

type* pointer_name;
 //or 
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)
Examples:
 int *n;
 RationalNumber *r;
 int **p; // pointer to pointer

Address Operator &:

Pointer in C

Than, Using C programming to perform this Copy Constructor operation you can follow any language.

So, try it your self on online compiler.

int a = 100;
//get the value, 
printf(“%d”,a); //prints 100
//get the memory address
Printf(“%d”,&a); //prints 1024

Address Operator &

#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
printf("The address of a is: %d“,&a);
printf("The address of b is: %d”,&b);
} 

Result is:
The address of a is: 1020
The address of b is: 1024

Pointer Variables:

int a = 100;
int *p = &a;
Printf(“%d %d %d %d”,a,&a,&p,p);

Result is:
100 1024
1024 1032

So, The value of pointer p is the address of variable a.
A pointer is also a variable, so it has its own memory address.

Pointer to Pointer:

Dereferencing Operator *:

int a = 100;
int *p = &a;
Printf(“%d”,a);
Printf(“%d”,&a);
Printf(“%d %d”,p,*p);
Printf(“%d”,&p);

output::
100
1024
1024 100
1032

Example:

void doubleIt(int x, 
 int * p)
{
*p = 2 * x;
}
int main(int argc, const 
char * argv[]) 
{
int a = 16;
doubleIt(9, &a);
return 0;
}



a gets 18

Another Example:

#include<stdio.h> 
void main ()
{
int value1 = 5, value2 = 15; 
int *p1, *p2; 
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2= value 
// pointed to by p1
p1 = p2; // p1 = p2 (pointer value copied)
*p1 = 20; // value pointed to by p1 = 20
printf(“%d %d”,value1,value2); 
getch(); 
} 

Another Pointer Example:

int a = 3;
char s = ‘z’;
double d = 1.03;
int *pa = &a;
char *ps = &s;
double *pd = &d; 
% sizeof returns the # of bytes…
cout << sizeof(pa) << sizeof(*pa)
 << sizeof(&pa) << endl;
cout << sizeof(ps) << sizeof(*ps)
 << sizeof(&ps) << endl;
cout << sizeof(pd) << sizeof(*pd)
 << sizeof(&pd) << endl;

So, for more update going to –learning points – Home | A better learning future starts here

For more OOCP concept go for –Class and Object – Learning Points

Leave a Reply

Your email address will not be published.