Académique Documents
Professionnel Documents
Culture Documents
Introduction to C Programming
Language
Cyrus Ashok Arupratan Atis
Lecturer, Dept. of ME, BUET
Sourav Saha
Lecturer, Dept. of ME, BUET
12/21/15
ME-172
C
Programming language
CLASS-8
DATE: December 21, 2015
12/21/15
Topic
Pointer
12/21/15
POINTERS
A pointer is a programming language object, whose value refers to
(or "points to") another value stored elsewhere in the computer
memory using its address. A pointer references a location in memory,
and obtaining the value stored at that location is known as
dereferencing the pointer.
A pointer is a variable which contains the address of another variable.
Organization of Memory
1
addres
s
i:
INTEGR TYPE
DATA
p
=&i
INTEGR TYPE
POINTER
0x2143
12/21/15
POINTERS
Declaration
For what type
of data will
the pointer be
used?
Dereferencing
variable
Syntax:
variable type *variable_name;
The target
variable
This syntax actually mimics the way how an ordinary variable is declared.
USE:
variable_name = &some_variable;
* and & are inverses and cancel each other out. That means *&y
is equivalent to y.
12/21/15
POINTERS
Declaration
Implementation example:
int x =1, y =2, z[10];
declaration
POINTERS
Simple examples
int x,*y;
x=1;
y=&x;
printf("x = %d\n",x);
printf("&x = %x\n",&x);
printf("y = %x\n",y);
printf("*y = %d\n",*y);
Format specifier
for printing
Hexadecimal
values
Output
12/21/15
POINTERS
Simple examples
int x,*y;
x=1;
y=&x;
printf("x = %d\n",x);
printf("&x = %x\n",&x);
printf("y = %x\n",y);
printf("*y = %d\n",*y);
*y = 9;
printf("after *y = 9 operation x =
%d\n",x);
12/21/15
Output
POINTERS
Pointers and Function Arguments
Ordinarily in C programming language arguments are passed to function by value. So,
there is no direct way to alter the actual variable with the help of a function. A function
merely can alter the copies of actual variables of the caller function.
However, by passing pointers to the variables of interest one can easily modify the actual
variables through function.
Return type function_name(pointer type *a);
Calling method
12/21/15
POINTERS
Pointers and Function Arguments
#include <stdio.h>
Method of
void sample(int *b);
declaration
void main()
{int x,*a;
x = 5;
a =&x;
printf("Before calling function x =
%d\n",x);
sample(a);
printf("\nAfter calling function x = %d",x);
}
void sample(int *b)
{
*b = 6;
}
12/21/15
10
POINTERS
Class
Performance 1!
Using pointer and a separate function write a program to swap
two variables. (e.g. if two variables are a=5 and b=10, swap their
values to a=10 and b=5).
12/21/15
11
POINTERS
#include <stdio.h>
void swap(int *a, int *b);
void main()
{int x,y,*p,*q;
x = 5;
y = 10;
p = &x;
q = &y;
printf("Before swapping x = %d\ty =
%d\n",x,y);
swap(p,q);
printf("\nAfter swapping x = %d\ty =
%d\n",x,y);
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
12/21/15
12
2
arr[1
]
3
arr[2
]
4
arr[3
]
13
12/21/15
pa
A[1]
&A[0] &A[1]
A[9]
pa+ 1
&A[9]
pa+ 9
14
If you are clever enough you have already guessed A[i] and *(A+i) point to
the same element.
Similarly pa[i] is identical to [pa +i].
12/21/15
15
16
12/21/15
17
#include <stdio.h>
int strcnt(char *a);
void main()
{int i,*p;
char s[12];
gets(s);
p = s;
i = strcnt(p);
printf("\nNumber of characters in input string is \t=
%d",i);
}
int strcnt(char *a)
{
int count=0;
for(count=0;*a!='\0';a++)
count++;
return count;
}
12/21/15
18
19
1 byte
20
12/21/15
21
CLASS PERFORMANCE
3!!
USING DYNAMIC MEMORY ALLOCATION DECLARE
AN INTEGER ARRAY AS POINTER TO TAKE 5
INTEGERS AS INPUTS. FIND OUT THEIR AVERAGE
IN A SINGLE ELEMENT FLOAT TYPE ARRAY
DECLARED AS POINTER.
12/21/15
22
23
POINTERS
There is a significant difference in strings (a type of array) and pointers. You
see, a string is a character type array and the compiler reserves certain
memory blocks for those. However, if a string is represented as a pointer,
this doesnt mean that these are fixed to some particular memory location.
Like other variables the same pointer can point to some other memory
location of choice.
12/21/15
24
25
int*number[4]
12/21/15
26
If we want to access third element of the fourth array we write the expression as
*(number[4]+2)
12/21/15
27
12/21/15
28
29
12/21/15
30
POINTERS: To Functions.
A very useful feature of pointer is, through it, you can pass one function as
argument of another function, or use the function as a variable!!!!!
12/21/15
31
POINTERS: To Functions.Swapping
void swaph(void (*pf)(int x, int y), int p, int q);
void swapg(int a, int b);
void main()
{ int p,q;
printf("Enter x = ");
scanf("%d",&p);
printf("Enter y = ");
scanf("%d",&q);
swaph(swapg, p, q);
}
void swaph(void(*pf)(int x, int y), int p, int q)
{ (*pf)(p,q);
}
void swapg(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
printf("\nAfter swapping x = %d and y =
%d\n",a,b);
}
12/21/15
ME 172 : C Programming Sessional
32
POINTERS
ASSIGNMENTS!
1) Write a simple C program using pointer that splits a string. Suppose, you
input a string Mustafizur/Rahman. The program must find the slash and
output as: First = Mustafizur; Last = Rahman.
2) Write your own version of strcmp() library function using pointers. That
means, the function will compare two strings and find out if these are similar
or not. For a change, your function should return character instead of integer.
If two strings match, the function should return y otherwise n.
12/21/15
33
Thank you
The only true wisdom is in knowing; you know nothing.
Socrates
12/21/15
34