Session 4 (Pointers)
Session 4 (Pointers)
SESSION 4 POINTER
CONTENTS
1 W H AT I S P O I N T E R 1
1 Introduction to pointer
1.1 Introduction to pointer
1 Introduction to pointer
1.1 Introduction to pointer
▪ To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the
address of that variable. For example &x gives us address of variable x.
▪ To declare a pointer variable: When a pointer variable is declared in C there must be a * before its
name.
▪ To access the value stored in the address we use the unary operator (*) that returns the value of the
variable located at the address specified by its operand. This is also called Dereferencing.
WHAT IS POINTER 1
1 Introduction to pointer
1.1 Introduction to pointer
CODE OUTPUT
DESCRIPTION
1 Introduction to pointer
1.1 Introduction to pointer
OUTPUT
CODE
DESCRIPTION
1 Introduction to pointer
1.1 Introduction to pointer
CODE OUTPUT
DESCRIPTION
1 Type of machine
1.2 Type of machine
1 Type of machine
1.2 Type of machine
CODE OUTPUT
DESCRIPTION
1 Pointer Arithmetic
1.3 Pointer Arithmetic
CODE OUTPUT
DESCRIPTION
1 Pointer Arithmetic
1.3 Pointer Arithmetic
CODE OUTPUT
DESCRIPTION
DESCRIPTION
DESCRIPTION
▪ Constants refer to fixed values that the program may not alter during its execution and put in .rodata
▪ Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.
▪ There are enumeration constants as well.
▪ String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals.
➢ Defining Constants
• There are two simple ways in C to define constants
▪ Using #define preprocessor.
▪ Using const keyword. Any variable cannot hack by pointer put in .rodata ,Any variable can hack by pointer put in stack
▪ Syntax:
WHAT IS POINTER 1
DESCRIPTION
DESCRIPTION
➢ Difference between const char *p, char * const p and const char * const p?
1) const char *ptr : This is a pointer to a constant character. You cannot change the value pointed by ptr,
▪ but you can change the pointer itself. “const char *” is a (non-const) pointer to a const char.
▪ NOTE: There is no difference between const char *p and char const *p as both are pointer to a const char and position
of ‘*'(asterik) is also same.
2) char *const ptr : This is a constant pointer to non-constant character. You cannot change the pointer p, but can
change the value pointed by ptr.
▪ NOTE: Pointer always points to same address, only the value at the location is changed.
3) const char * const ptr : This is a constant pointer to constant character. You can neither change the value pointed by
ptr nor the pointer ptr.
NOTE: char const * const ptr is same as const char *const ptr.
WHAT IS POINTER 1
DESCRIPTION
DESCRIPTION
DESCRIPTION
➢ Dangling pointer:
▪ A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
▪ Function Call,Variable goes out of scope.
➢ Void pointer:
▪ Void pointer is a specific pointer type – void * – a pointer that points to some data location in storage, which doesn’t
have any specific type.Void refers to the type. Basically the type of data that it points to is can be any. If we assign address
of char data type to void pointer it will become char Pointer.
Important Points
▪ void pointers cannot be dereferenced. It can however be done using typecasting the void pointer.
▪ Pointer arithmetic is not possible on pointers of void due to lack of concrete value and thus size.The C standard doesn’t
allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1
➢ NULL Pointer:
▪ NULL Pointer is a pointer which is pointing to nothing. In case, if we don’t have address to be assigned to a pointer, then
we can simply use NULL.
▪ Important Points
• NULL vs Uninitialized pointer – An uninitialized pointer stores an undefined value. A null pointer stores a defined
value, but one that is defined by the environment to not be a valid address for any member or object.
• NULL vs Void Pointer – Null pointer is a value, while void pointer is a type
WHAT IS POINTER 1
➢ wild pointer:
▪ A pointer which has not been initialized to anything (not even NULL) is known as wild pointer. The pointer may be
initialized to a non-NULL garbage value that may not be a valid address.
WHAT IS POINTER 1
DESCRIPTION
▪ No error
WHAT IS POINTER 1
DESCRIPTION
DESCRIPTION
1 Double pointer in c
1.7 Double pointer in c
▪ Declaring Pointer to Pointer is similar to declaring pointer in C. The difference is we have to place an additional ‘*’ before the
name of pointer.
▪ Syntax:
WHAT IS POINTER 1
1 Double pointer in c
1.7 Double pointer in c
CODE OUTPUT
DESCRIPTION
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable
code.
2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
3) A function’s name can also be used to get functions’ address. For example, in the below program, we have removed address
operator ‘&’ in assignment. We have also changed function call by removing
4) Like normal pointers, we can have an array of function pointers. Below example in point 5 shows syntax for array of pointers.
5) Function pointer can be used in place of switch case. For example, in below program, user is asked for a choice between 0
and 2 to do different tasks.
6)Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function.
WHAT IS POINTER 1
DESCRIPTION
DESCRIPTION
DESCRIPTION
DESCRIPTION
➢
What are near, far and huge pointers?
These are some old concepts used in 16 bit intel architectures in the days of MS DOS, not much useful anymore.
▪ Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we
can only access 64kb of data at a time.
▪ A far pointer is typically 32 bit that can access memory outside current segment. To use this, compiler allocates a segment
register to store segment address, then another register to store offset within current segment.
Like far pointer.
▪ huge pointer is also typically 32 bit and can access outside segment. In case of far pointers, a segment is fixed.