

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lab work is assigned by Nauman Shamim at Pakistan Institute of Engineering and Applied Sciences, Islamabad (PIEAS) for Computer Fundementals course. It covers following topics: Lab, Pointers, Arrays, Functions, Reference, Operator, RAM, Difference, Internals, Reset
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Read the document in sequence, it is necessary to understand the contents for doing the task given at the end.
int A[5]={10,20,30,40,50};
void reset( int B[], int size){ for ( int a=0;a<size;a++) B[a]=0; } [ Header Files ] main(){ for ( int a=0;a<size;a++) cout <<A[a]<<”, “; cout <<endl; reset(A,5); for ( int a=0;a<size;a++) cout <<A[a]<<”, “; } Output 10 , 20 ,30 ,40 , 0 , 0 , 0 , 0 , 0
Internals of call to reset(int [],int) reset(A,5) Æ reset( 0x ff00 ,5)
opt here refers to operation, when array B is processed inside the function reset(), changes to B are actually to the location to which array B is stored, in this case the array B holds the memory address passed as a parameter in reset() function i.e. memory address of array A.
Computer programs are loaded into RAM (Random Access Memory), all variables and functions are allocated memory in RAM, as discussed in the class each byte of RAM has an address. In C++ reference operator & is used to access memory address of any variable defined in the program.
int a=10; cout <<”Value of a = “<<a<<endl; cout <<”Address of a = ”<<&a; Output Value of a =10; Address of a= 0x00ff [not original value] When a variable is declared an identifier/name is associated with the variable, the variable created in the memory and can store allowed type of data, in C++ reference to a variable is an identifier that refers to a variable already defined, any changes through reference to a variable are reflected in variable itself, in easy words a reference to a variable can be said second name of the variable or alias
Memory allocated to A
int a=10; int ref_a=&a; cout<<”Value of a = “<<a<<endl; cout<<”Address of a = ”<<&a<<endl;; cout<<”value of ref_a = ”<<ref_a<<endl; cout<<”Address of ref to a =”<<&ref_a<<endl; ref_a=100; cout<<”value of a = ”<<endl; cout<<”Value of ref_a = “<<ref_a; Output Value of a = Address of a= 0x00ff [not original value] Value of ref to a = Address of ref to a = 0x00ff [not original value] Value of a = 100 Value of ref_a= Reference Declaration type &reference_name = variableName; When a reference is declared it should must be initialized by a variable of the same type i.e. reference of type integer can only be initialized with a integer variable
A pointer variable is used to store the memory address of a variable; pointer variables have “Pointer to T” structure, where T is the type of the object to which pointer points.
Line 5 declares and initializes a pointer variable i.e. *int pn : declares pointer variable pn , its type is integer which means that it will be used to store
memory addresses of integer variables, the next part of the statement at line 5 ( =&n ) assigns pn the memory address of an integer variable n. It can be said that pn is a pointer to n
Note: It is not necessary to initialize the pointers when declared Pointer variables occupy space in memory unlike references.
If pn points to n , we can obtain the value of n directly from p ; the expression *pn evaluates to the value of n. This evaluation is called “dereferencing the pointer” pn , and the symbol ***** is called the dereference operator
Tasks 1-Declare and initialize an array of 10 integers and print the address of the array and each element of the array.
2-Write a program to demonstrate that pointers can indirectly change the value of a variable
3-Write a program that reference variables point to same memory location as the actual variable