Topic : Pointer
Topic : Pointer
Pointer
Organization of Memory
1 2 3 4
address i: INTEGR TYPE DATA
0x2143
USE:
variable_name = &some_variable;
* and & are inverses and cancel each other out. That means *&y is equivalent to y.
int x =1, y =2, z[10]; //two integers and one array declaration
Output
19-Dec-17 ME 172 : C Programming Sessional 5
POINTERS
Simple examples
int x,*y;
x=1;
y=&x;
printf("x = %d\n",x);
printf("&x = %x\n",&x); Output
printf("y = %x\n",y);
printf("*y = %d\n",*y);
*y = 9;
printf("after *y = 9 operation x = %d\n",x);
19-Dec-17 ME 172 : C Programming Sessional 6
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.
Before going into the discussion, lets recall what we learned about arrays.
1 2 3 4
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].
There are quite a few functions that we can use for this purpose, alloc(), calloc(),
realloc() and malloc() are examples. To empty the memory location we use afree()
and free(). Only malloc() is discussed here.
4 bytes
1 byte
#include <stdio.h>
void main()
{int i,n,*p;
n = 5;
p =(int*) malloc(n*sizeof(int));
for(i=0;i<n;i++) scanf("%d",p+i);
for(i=0;i<n;i++)
printf("\n%d",*(p+i));
}
int*number[4]
number[0]
number[1]
number[2]
number[3]
0 1 2
If we want to access third element of the fourth array we write the expression as
*(number[3]+2)
In order to sort strings we can use character type arrays of pointers effectively. In such
cases, the pointer point to the first character of each string. Hence, we can compare
strings comfortably.
We can use strcmp() function for our aid. If strcmp(string1, string2) gives:
I. Negative value, first string precedes the second.
II. Zero, both strings are same.
III.Positive value, second string precedes the first.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{ int i;
char *temp,*nam[3] = {“United", “Barca", “Real"};
for(i=0;i<3;i++){if(strcmp(nam[i],nam[i+1])>0) {
temp = nam[i];
nam[i] = nam[i+1];
nam[i+1]= temp;}
}
printf("\n\n\n********\n\n");
for(i=0;i<3;i++) puts(nam[i]);
}
19-Dec-17 ME 172 : C Programming Sessional 21
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!!!!!
Let us call the argument function as guest and caller function as host.
For the guest function method of declaration is usual: data type func_name(arg1,arg2);
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’.
Socrates