Lec3 - Working With Pointers
Lec3 - Working With Pointers
programming
Lecture 3: Working with pointers
By: Dr. Ahmad Barghash
Q1
• Write a program that defines a structure that holds 3 variables (string, float and integer).
In the main function declare an array of this structure to hold 3 structures. The structure
variables correspond to Name, Grade and age. Print the name of the youngest student
and print the name of the student with maximum grade.
1. Define a structure
2. Declare an array of the structure
3. Fill the array from user
4. Find youngest
5. Find student with max grade
#include <stdio.h>
#include<string.h>
struct entry for(i=0;i<5;i++)
{ {
if(students[i].Grade>max)
Solution
char Name[15];
int Age; {
float Grade; max=students[i].Grade;
}; max_ind=i;
}
int main (void)
{ if(students[i].Age<young)
int i; {
struct entry students[3]; young=students[i].Age;
for(i=0;i<3;i++) young_ind=i;
{ }
printf("enter student info \n"); }
gets(students[i].Name);
scanf("%i printf ("student with max grade is %s\n",
%f",&students[i].Age,&students[i].Grade); students[max_ind].Name);
} printf ("youngest student is %s\n",
students[young_ind].Name);
float max=students[0].Grade;
}
int max_ind=0;
int young=students[0].Age;
int young_ind=0;
Write a program that declares and fills two strings
from user input then print the string with
maximum number of character m
Q2 1.
2.
Declare two strings
Fill strings from user
3. Count character m in each string
4. Print the correct string
#include <stdio.h>
#include<string.h>
int main (void)
{
char s1[100],s2[100];
int c1,c2,i;
printf("Fill the two strings\n");
gets(s1);
gets(s2);
for(i=0;i<'\0';i++)
Solution {
if(s1[i]=='m')
c1+=1;
if(s2[i]=='m')
c2+=1;
}
if(c1>c2)
puts(s1);
else
puts(s2);
}
Pointers
Example:
int count =10;
int *int_pointer; // note the *
int_pointer = &count; //note the &
First program
#include <stdio.h>
int main (void)
{
int count = 10, x;
int *int_pointer;
int_pointer = &count;
x = *int_pointer;
printf ("count = %i, x = %i\n", count, x);
return 0;
}
Call by Call by
value reference
#include<stdio.h>
void interchange(int *num1,int *num2)
{
int temp;
temp = *num1;
*num1 = *num2;
Call by }
*num2 = temp; The output is:
Number 1 : 70
int num1=50,num2=70;
interchange(&num1,&num2);
printf("\nNumber 1 : %d",num1);
printf("\nNumber 2 : %d",num2);
return(0);
}
Calling summary
return 0;
}
#include <stdio.h>
int main () { Var address is bffd8b3c
int var = 20; // actual variable declaration
int *ip; // pointer variable declaration
#include <stdio.h>
int main(){
char c[4]; %x: lower-case hexadecimal
int i;
for(i=0;i<4;++i){
printf("Address of c[%d]=%x\n",i,&c[i]);
}
return 0;
}
#include <stdio.h>
int main(){
char c[4];
int i;
for(i=0;i<4;++i){
printf("Address of c[%d]=%x\n",i,&c[i]);
}
return 0;
}
• In arrays of C programming, name of the array always points to
the first element of an array
• The address of first element of an array is &arr[0]
• &arr[0] is equivalent to arr
• Similarly &arr[1] is equivalent to (arr+1)
• Similarly &arr[2] is equivalent to (arr+2)……
Pointer arithmetic
int arr[4];
Example
//Program to find the sum of six numbers with arrays and pointers
The output is:
#include <stdio.h>
Enter 6 numbers:
int main(){
int i,class[6],sum=0;
2
printf("Enter 6 numbers:\n"); 3
for(i=0;i<6;++i){ 4
scanf("%d",(class+i)); // (class+i) is equivalent to &class[i] 5
sum += *(class+i); // *(class+i) is equivalent to class[i] 3
} 4
printf("Sum=%d",sum); Sum=21
return 0;
}
Another example
#include <stdio.h>
int main(){
The output is:
Enter elements: 1
int data[5], i;
2
printf("Enter elements: ");
3
for(i=0;i<5;++i) 5
scanf("%d",data+i); 4
printf("You entered: "); You entered: 1
for(i=0;i<5;++i)
2
3
printf("%d\n",*(data+i));
5
return 0;
4
}
#include <stdio.h>
int main () {
/* an array with 5 elements */
float balance[5] = {1000.0, 2.0, 3.4, 17.0,
50.0}; The output is:
the array
*(p + 2) : 3.400000
/* output each array element's value */ *(p + 3) : 17.000000
#include <stdio.h>
int main (void)
{
struct date {
containing
{
int *p1;
Linked lists
n1.value = 100;
n2.value = 200;
n3.value = 300;
n1.next = &n2;
n2.next = &n3;
i = n1.next->value;
printf ("%i ", i);
printf ("%i\n", n2.next->value);
return 0;
}
Linked list vs double linked list
Assignment 1
• Modify your previous program adding a search method. The program should ask the user
about the element he wants to search for and return the position of it in case found.
• Tips:
• A pointer value is set to NULL in case it is not used. The prev of the first element is
NULL and the next of the last element is NULL
• To navigate through the structure you are advised to have a temporary structure to
report the current position
• If x,y are structures of the same type, x=y makes x a copy of y with all dependences
including pointer connections