0% found this document useful (0 votes)
322 views

Pointers C++ Slides

The document discusses pointers in C/C++. It defines pointers, address operators, passing arguments by reference, one-dimensional and two-dimensional arrays, and examples of functions that operate on arrays using pointers. Problems at the end involve defining functions to multiply matrices, rotate a matrix, sum matrix diagonals, check arrays for vowels, and combine two arrays.

Uploaded by

Hafiz Younas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
322 views

Pointers C++ Slides

The document discusses pointers in C/C++. It defines pointers, address operators, passing arguments by reference, one-dimensional and two-dimensional arrays, and examples of functions that operate on arrays using pointers. Problems at the end involve defining functions to multiply matrices, rotate a matrix, sum matrix diagonals, check arrays for vowels, and combine two arrays.

Uploaded by

Hafiz Younas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ACP course outline

• Pointers
• User defined Data types
• Structure
• Link List
• Object oriented programming
• Classes
• Constructors and Destructors
• Inheritance
• Friend functions
• File Handeling
Address or Reference Operator(&) /
Pointer or De-reference operator(*)
• In C and C++ & sign is used as reference or address
operator. When used with a variable as prefix, it return
the address of that variable.
Example
Int x; cout<<&x;
This operator is commonly used to pass a reference as
argument instead of a value to a function to update a
variable data.
• Asterisk symbol (*) is an Operator used to declare
pointer variable. It also return the value stored at
memory location to which a pointer is pointing.
Address or Reference Operator(&)
Passing a value and reference to a function

void swap(int x, int y) void swap(int &x, int &y)


{ {
int tmp=x; int tmp=x;
x=y; x=y;
y=tmp y=tmp
} }
Pointer Variable
• A pointer variable is a variable that can store
memory address of other variable as their value.
• A pointer variable is declared by using * operator.
Example int *x; or char *z; int y;
• It does not mean that x can store in an integer value.
What it mean, x is a variable that can store an
address of an integer variable. ie x=&y;
• You can not assign a value directly to a pointer ie
*x=20 or x=20;
• Similarly z is a variable that can store the address of
a character variable.
• A pointer may be initialized as 0, NULL or an
address. ie int *p,x; p=0 or p=NULL or p=&x
• A pointer should be initialized either when
declared or in an assignment statement. ie int
*p=0; or p=0;
• When assigning a variable to a pointer, &
operator is used as prefix to that variable
Int x=10,*y y=&x;
• * Operator gives the value at address to which a
pointer is pointing.
• cout<<*y the output will be 10;
Pointers and One Dimensional Arrays
Suppose int *p, ary[5]={2,4,6,8,10};
• To pass an array to pointer p we can proceed as:
p=ary or p=&ary[0] this statement will assign the address
of ary[0] to p:
p
ary 2 4 6 8 10

*p or *(p+0) represent the value at ary[0] ie 2


*p+1 represent the value ary[0]1 ie 3
and *(p1) represent the value at ary[1] ie 4
p increment in the pointer value ie physically move
the pointer to next memory location.
• *p; pp1; *p*p; *p*(p1) have the same
effect.
Passing 1D array of pointer
in a function.
1-D array and pointer

Void Disp(int *p, int arysize) void Disp(int *p, int arysize)
{ {
for(int i=0;i<arysize;i++) while(arysize-->=1)
{ {
cout<<*(p+i)<<endl; cout<<*p++<<endl;
} }
} }
main()
{
int array[5]={1,2,3,4,5},*p;
Disp(array,5);
system("pause");
}
Define a function factorial that can read data from
an array to a pointer and print factorial of each
elements of array
void factorial(int *p, int arysize)
{
long double fact;
while(arysize >=1) main()
{ { int array[5]={1,2,3,4,5},*p;
fact=1; factorial(array,5);
for(int n=*p;n>1;n--) system("pause");
{ }
fact=fact*n;
}
cout<<*p<<“!=“<<fact<<endl;
p++;
arysize--;
}
}
Pointers and Two Dimensional Arrays
int *p, mat[2][3]={{2,4,6},{8,10,12}};
The 2D array data is actually stored in 1D array format in memory.

2 4 6 8 10 12
p=mat[0] or p=&mat[0][0] or p=*mat assign the address of 0th element
to p.
*p or *(p+0+0) represent 0th element of array ie 2. where 0 0 represent
ith and jth indexes. ie *(p+i+j);

void dispmat(int *p, int row, int col)


{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<*(p+col*i+j)<<“\t”;
}
cout<<endl
}
}
Examples

Function that print the Function that change diagonal


transpose of a matrix element of a square matrix to 1.

void diagonal1(int *p, int r, int c)


void transpose(int *p, int r, int c) {
{ for(int i=0;i<r;i++)
for(int i=0;i<c;i++) {
{ for(int j=0;j<c;j++)
for(int j=0;j<r;j++) {
{ if(i==j)
{
cout<<*(p+c*j+i)<<"\t";
*(p+c*i+j)=1;
} }
cout<<endl; }
}
} }
}
Problem
• Define a function using pointers that multiply two
matrices.
• Define a function using pointer that rotate a given matix
by an angle of 90 clockwise and anti-clockwise.
• Define a function using pointers that return the sum of
diagonal elements of a square matrix.
• Define a function using pointer that can check each
character of an array and print out vowel character.
• You are given two arrays of type integer. Write a program
using pointer to combine these arrays into a single array
by mixing the elements.
• Ary[m]={1,2,3,4,5….m}, ary2[n]={6,7,8,9,10…..n};
• Ary[m+n]={1,6,2,7,3,8,4,9,5,10…..m,n};

You might also like