Pointers Arrays Functions-Computer Fundementals-Lab10b-Lab Assignment, Exercises of Computer Fundamentals

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

2011/2012

Uploaded on 07/04/2012

waniaa
waniaa 🇵🇰

7 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ComputingFundamental DocumentcreatedbyNaumanShamim
1PakistanInstituteofEngineering&AppliedSciences
DepartmentofComputerandInformationSciences
LAB-10-POINERS ARRAYS FUNCTIONS
INSTRUCTIONS
Read the document in sequence, it is necessary to understand the contents for doing the task given at the end.
Anarraycanbepassedtoafunctionjustlikeother
datatypeswithoneexceptioni.e.thearrayscan
onlybepassedasreference,anychangesinthe
arraybyfunctionwillbereflectedinoriginalarray
passed.Reason:Anarrayispassedtoafunctionby
passingthenameofthearrayonly,anarrayname
serverstwopurpose1‐Itrepresentsthewholearray
2‐Itholdsthestartingaddressofthememoryblock
allocatedtothearraywhichisalsothememory
addressofthefirstelementofthearray.
EXAMPLE1:
int A[5]={10,20,30,40,50};
AÆ&A[0]Æ
A[1]Æ
A[2]Æ
A[3]Æ
A[4]Æ
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 ,50
0 , 0 , 0 , 0 , 0
Internals of call to reset(int [],int)
reset(A,5) Æ reset(0xff00,5)
opt at B[0]=0 Æ 0xff00 A[0]=0
opt at B[1]=0 Æ 0xff04 A[1]=0
opt at B[2]=0 Æ 0xff08 A[2]=0
opt at B[3]=0 Æ 0xff0b A[3]=0
opt at B[4]=0 Æ 0xff0f A[4]=0
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.
EXAMPLE2:
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
MemoryallocatedtoA
0
x
ff0
0
10
0
x
ff0420
0
x
ff0830
0
x
ff0b40
0
x
ff0f50
ARRAYSANDFUNCTIONS
REFERENCES&REFERENCEOPERATOR
Docsity.com
pf2

Partial preview of the text

Download Pointers Arrays Functions-Computer Fundementals-Lab10b-Lab Assignment and more Exercises Computer Fundamentals in PDF only on Docsity!

Computing Fundamental Document created by Nauman Shamim

1 Pakistan Institute of Engineering & Applied Sciences

Department of Computer and Information Sciences

LAB -10- P OINERS ARRAYS FUNCTIONS

I NSTRUCTIONS

Read the document in sequence, it is necessary to understand the contents for doing the task given at the end.

An array can be passed to a function just like other

data types with one exception i.e. the arrays can

only be passed as reference, any changes in the

array by function will be reflected in original array

passed. Reason : An array is passed to a function by

passing the name of the array only, an array name

servers two purpose 1 ‐ It represents the whole array

2 ‐ It holds the starting address of the memory block

allocated to the array which is also the memory

address of the first element of the array.

E XAMPLE‐1:

int A[5]={10,20,30,40,50};

AÆ & A[0]Æ

A[1]Æ

A[2]Æ

A[3]Æ

A[4]Æ

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 at B[0]=0 Æ 0xff00 A[0]=

opt at B[1]=0 Æ 0xff04 A[1]=

opt at B[2]=0 Æ 0xff08 A[2]=

opt at B[3]=0 Æ 0xff0b A[3]=

opt at B[4]=0 Æ 0xff0f A[4]=

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.

E XAMPLE‐2:

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

A RRAYS AND FUNCTIONS

REFERENCES & REFERENCE O PERATOR

Docsity.com

Computing Fundamental Document created by Nauman Shamim

2 Pakistan Institute of Engineering & Applied Sciences

Department of Computer and Information Sciences

E XAMPLE‐3:

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.

E XAMPLE‐4: USING POINTER VARIABLES

  1. main(){
  2. int n=44;
  3. cout<<” value of n=”<<n<<endl;
  4. cout<<”address of n=”<<&n<<endl; *5. int pn=&n;
  5. cout<<”value of pn =”<<pn<<endl;
  6. cout<<”address of pn”<<&pn<<endl;
  7. } Output value of n = address of n =0x value of pn =0x address of pn = 0x0ff

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

E XAMPLE‐5: DEREFERENCING A POINTER

  1. main(){
  2. int n=44;
  3. cout<<” value of n=”<<n<<endl;
  4. cout<<”address of n=”<<&n<<endl; *5. int pn=&n;
  5. cout<<”value of pn =”<<pn<<endl;
  6. cout<<”address of pn”<<&pn<<endl;
  7. cout<<”value of pn”<<pn<<endl;
  8. } Output value of n = address of n =0x value of pn =0x address of pn = 0x0ff value of *pn = 44

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

P OINTER VARIABLE & D EFERENCE OPERATOR

Docsity.com