Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

UNIVERSITY OF DAR ES SALAAM

PROGRAMMING IN C
POINTERS
Masoud H. Mahundi
mmahundi97@gmail.com or mahundi.masoud@udsm.ac.tz
0713832252 or 0768832424
Introduction
 Generally, our computer storages are like;

address Useful Memory Address Useful Memory Address Useful Memory


1 1000
2 …
3 …
…. … 566900
…. ….
…. …

 The concept of pointers helps to deal with data through their memory addresses
 We say “multiply what is in the following memory address”
 Reading the address is done through “&” - ampersand is an address operator
Accessing Memory Address
 A normal Variable store data
 A pointer variable stores memory address of the data stored
 A pointer is a variable that points to or references a memory location in which data is stored
 Its value is the address of another variable, i.e., direct address of the memory location”
 A variable that expects to only store memory addresses and not values - pointer variables

 Declaration
 Syntax <<data_type>> * <<variable_name>>
 data_type for the data element to be stored in the memory
 variable_name follows the same identifier naming conventions
 Example: float * variable1;
 The asterisk (*) tells the compiler that var is a pointer variable
Declaration
 For example

 int *varaddr;

 int age; // a memory space is reserved

 varaddr = &age // the pointer gets an address to point to

1. #include<stdio.h>
2. main(){
3. int *b, c, a=5;
4. b = &a;
5. printf("The Address is %u and Value is %d",b,a);
6. printf("\n");
7. }
Dereferencing
1. #include <stdio.h>
2. main() {
3. int num = 123;
4. float x = 14.56;
5. int *intptr; //intptr is a pointer, expects only addresses
6. float *floptr; //floatptr is a pointer, takes addresses only
7. intptr=&num; //intptr now takes the address of num
8. floptr=&x;
9. printf("Num %d at address %u\n",*intptr,intptr);
10.printf("Value %.2f at address %u\n",*floptr,floptr);
11.}
12.//Dereferencing – accessing the value through the memory address
Declaration
 Three basic types of pointers

 Wild pointers: a pointer that points to nothing – not advisable

 float * pointvar;

 Null pointers: a pointer pointing to NULL

 float * pointvar = NULL;

 Void pointers: pointer without any specific data type

 void* pointvar;

 It can take any data type, later


Pointers Arithmetics
 They can be computed 1. #include <stdio.h>

as any other variables. 2. main() {


3. int sum, prod;
4. int num1 = 13;
5. int num2 = 11;
6. int *intp1;
7. int *intp2;
8. intp1 = &num1;
9. intp2 = &num2;
10. sum = *intp1 + *intp2;
11. prod = *intp1 * *intp2;
12. printf("Sum: %d and Product: %d\n", sum,
prod);
13.}
Wake Up
1. Declare a variable of type float

2. Assign 15.3 to the variable

3. Declare a pointer variable of type float

4. Assign the address of the first variable to the pointer variable

5. Print the address of the first variable

6. Print the value of the first variable through reading from the address in the pointer
variable
Pointers with Arrays
 Pointers can be used to access elements within an array

 Two characteristics of arrays make it possible to do that


1. The name of the array holds the address of the first element of the array.

2. Elements of an array are adjacent to each other

myArray[0] myArray[1] myArray[2] myArray[3]


7 9 11 13

 printf(“Address is %u\n”, myArray) will print address for index 0


 This is because the name of the array => address of the first element
Pointers with Arrays
myArray[0] myArray[1] myArray[2] myArray[3]
 This shows the significance of
7 9 11 13
specifying data type in declaring
pointers
ELEMENT ADDRESS DEREFERENCING
 So that it is known how many
myArray[0] myArray *myArray
bytes the data is stored in
myArray[1] myArray + 1 *(myArray + 1)
 When we increment a pointer we
myArray[2] myArray + 2 *(myArray + 2)
increase the pointer by one block
myArray[3] myArray + 3 *(myArray + 3)  With Data type we know how big
is the”block”
Pointers with Arrays

//MANUAL LISTING
#include <stdio.h>
main(){
int theArr[] = {5, 7, 9, 11, 13};
printf("Array: %d Pointer: %d\n",theArr[0], *theArr);
printf("Array: %d Pointer: %d\n",theArr[1], *(theArr+1));
printf("Array: %d Pointer: %d\n",theArr[2], *(theArr+2));
printf("Array: %d Pointer: %d\n",theArr[3], *(theArr+3));
printf("Array: %d Pointer: %d\n",theArr[4], *(theArr+4));
}
Pointers with Arrays

//LISTING WITH LOOP


#include <stdio.h>
main(){
int theArr[] = {5, 7, 9, 11, 13};
int k;
for(k=0; k<5; k++){
printf("The Address is %x\t",(theArr+k));
printf("The Value is %d\n",*(theArr+k));
}
}
Introduction
#include<stdio.h>
int my_array[] = {23, 12, 54, 0, -9, 37};
int *ptr;
main(){
What will be the
int i; Output?
ptr = my_array;
printf("\n");
for(i=0;i<6;i++){
printf("my_array[%d] = %d ",i,my_array[i]);
printf("ptr + %d = %d\n",i, *(ptr+i));
}
}
Array of String
 Comes through an array of pointers
char strarray; // a declaration of a character

char* strarray; // a declaration of a pointer to a character

char* strarray[5]; // an array of pointers to characters

char* strarray[3] = {“one”, “two”, “three”};


Array of String
 The following program tries and print the array of strings

#include<stdio.h>
main(){
int i;
char *strArr[] = {"one", "two", "three"};
for(i=0; i<3; i++)
printf("%s ", strArr[i]);
}
Array of String
 Reading is a somehow different
1. #include<stdio.h>
 Example below reads 2. #include<stdlib.h>
string from the user 3. main(){
4. char *strArr[5];
5. int k;
6. for(k=0; k<5;k++){
7. printf("Enter a string: ");
8. strArr[k] = (char *) malloc (10);
9. scanf("%s", strArr[k]);
10. }
11. }
Array of String
#include<stdio.h>
#include<stdlib.h>
main(){
char *strArr[5];
int k;
for(k=0; k<5;k++){
printf("Enter a string: ");
strArr[k] = (char *) malloc (10);
scanf("%s", strArr[k]);
}
for(k=0; k<5;k++){
printf("%s, ", strArr[k]);
}
}
Pointers in Functions

//NORMAL ARGUMENTS
#include <stdio.h>
int* convert(int *inArray, int n){
int i;
for(i=0;i<n;i++){
*(inArray + i) = 3**(inArray + i);}
return inArray;
}

You might also like