C Programming Topic: Pointers Module 1: Pointer Basics
C Programming Topic: Pointers Module 1: Pointer Basics
Topic: Pointers
Module 1: Pointer Basics
Table of Content:
S. No Concept/Title
1 Concept of Pointer
1.1 Concept of Indirection
1.2 What is a Pointer?
1.3 Why Pointers?
1.4 The & and * Operators
2 Using Pointers
2.1 Declaring Pointers
2.2 Initializing Pointers
2.3 Accessing variables through Pointers
2.4 Common Errors While Using Pointers
2.5 Pointer to Pointer
3 Pointer Operations
3.1 Pointer Expressions
3.2 Pointer Arithmetic
3.3 Pointer & Arrays
3.4 Array of Pointers
3.5 Segmentation Fault
1. Concept of Pointer
1.1 Concept of Indirection
ANS: This may lead to serious run time error. p is a pointer variable but it is not
referring (pointing) to any other variable. If a value is assigned directly, it may
use any unauthorized area in the memory which may lead to error or system
crash.
#include<stdio.h>
int main()
{
int *p, x, num; // Two normal variables and one pointer
to integer variable are declared
p = &x; // Pointer variable is assigned the
address of
x = 100; // A value 100 is assigned to x
num = *p + 100; /* 100 is added to value of variable
referenced by p(100),
and the result (200) is assigned to num
*/
p= #
printf("%d", *p);
return 0;
}
Q) Can multiple pointers point to same variable?
Yes, any number of pointers can point to same variable
int main()
{
int *p; /* p is not referring to any variable */
p=0; /* p is not a dangling pointer now */
}
It is dangerous to leave a pointer without initialization
It is always necessary to initialize pointers immediately after
declaration
If there is no value to assign immediately, a pointer may be
initialized with NULL(0)
int main()
{
char *st= malloc(20); /* st points to allocated array */
free(st); /* don’t forget to deallocate when finished */
}
2.5 Pointer to Pointer
A pointer can store address of another pointer
That means, one pointer can point to another pointer, which in
turn points to another pointer
Declaring a pointer to pointer
Expression Meaning
x = *p1 + *p2; x is sum of values stored in variables pointed by p1
and p2
y = 2 * *p; y is product of 2 and value stored in variable pointed
by p
*p++; Value stored in variable pointed by p is incremented
a = 5 + *p1/*p2;
In this expression, the compiler treats ‘/*’ symbols as beginning of
the comment. This may lead to syntactical errors in the program.
The safe way of writing above expression is:
a = 5 + *p1/*p2;
a= 5 + (*p1)/(*p2);
Questions:
Identify errors if any in the following expressions. Rewrite the
expressions with and without using brackets.
2) total = total/*p1**p2;
Ans: Error. All characters after /* are treated as comments.
4) Count + = *p;
Ans: No error. But, alternatively we can write as:
count + = *p;
or
count + = (*p);
Scale Factor:
Question:
100
Base Address
We know that:
o Array name is a pointer which contains the address of first
element of the array
o Elements of an array are stored in adjacent memory locations
Reason:
o x is an integer array.
o Size of integer is 2 bytes.
o First element occupies memory locations 100 and 101.
o Next element occupies memory locations 102 and 103, so
on...
Question:
1000 N E W D E L H I \0
1010 M U M B A I \0
1020 C H E N N A I \0
1030 K O L K A T A \0
Alternatively, we can use array of pointers as shown below
char *mptr[4]= { “NEW DELHI”, “MUMBAI”, “CHENNAI”,
“KOLKATA”};
Solved Problem:
Write a program to sort the set of strings in alphabetical order using
array of pointers
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void sort(int n, char *x[]);
main()
{
int i, n = 0;
char *x[10];
printf("Enter each string on a separate line\n\n");
printf("Type \'end\' when finished\n\n");
/* read the list of strings */
do {
/* allocate memory */
x[n] = (char *) malloc(12 * sizeof(char));
printf("string %d:", n + 1);
scanf("%s", x[n]);
}while(strcmp(x[n++], "end"));
sort(n - 1, x);
for(i = 0; i < n; ++i)
printf("\nString %d %s", i + 1, x[i]);
}
void sort(int n, char *x[])
{
char *temp;
int i, item;
for (item = 0; item < n - 1; ++item)
/* Get lowest of all strings */
for (i = item + 1; i < n; ++i)
if (strcmp(x[item], x[i]) > 0){
temp = x[item];
x[item] = x[i];
x[i] = temp;
}
return;
}
3.5 Segmentation Fault
What is a segmentation fault?
When your program runs, it has access to certain portions of
memory.
Your program is only allowed to touch memory that belongs to
it.
Any access outside that area will cause a segmentation fault.
Segmentation faults are commonly referred to as segfaults.
There are four common mistakes that lead to segmentation
faults:
o dereferencing a NULL or uninitialized pointer
o dereferencing a pointer that has been freed
o dereferencing a pointer that has gone out of scope of an
array
o a recursive function that uses all of the stack space
Summary:
7) Count + = *p;
Ans: No error. But, alternatively we can write as:
count + = *p;
or
count + = (*p);