Professional Documents
Culture Documents
Lecture 7 - Pointers
Lecture 7 - Pointers
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;
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;
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=# //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
float * pointvar;
void* pointvar;
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
//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
#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;
}