0% found this document useful (0 votes)
36 views35 pages

Session 4 (Pointers)

Pointers store the address of variables or memory locations. To use pointers in C, we use the & operator to get the address of a variable and the * operator to dereference a pointer and access the value at that address. Pointers allow passing arguments to functions by reference so the function can modify the original variables. There are different types of pointers like constant pointers that cannot be used to modify the value of a variable and null pointers that point to invalid memory locations. Pointers allow many powerful programming techniques in C but must be used carefully to avoid bugs.

Uploaded by

Luka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views35 pages

Session 4 (Pointers)

Pointers store the address of variables or memory locations. To use pointers in C, we use the & operator to get the address of a variable and the * operator to dereference a pointer and access the value at that address. Pointers allow passing arguments to functions by reference so the function can modify the original variables. There are different types of pointers like constant pointers that cannot be used to modify the value of a variable and null pointers that point to invalid memory locations. Pointers allow many powerful programming techniques in C but must be used carefully to avoid bugs.

Uploaded by

Luka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

AMIT LEARNING

SESSION 4 POINTER
CONTENTS

1 W H AT I S P O I N T E R 1

1.1 Introduction to Pointers. 1

1.2 Type of machine

1.3 Pointer Arithmetic. 1

1.4 Passing pointer to function. 1

1.5 What is constant and constant with pointer 1

1.6 Dangling, Void , Null and Wild Pointers. 1

1.7 Double pointer in c. 1

1.8 How to declare a pointer to a function. 1

1.9 What are near, far and huge pointers? 1


WHAT IS POINTER 1

1 Introduction to pointer
1.1 Introduction to pointer

➢ Pointers store address of variables or a memory location.


WHAT IS POINTER 1

1 Introduction to pointer
1.1 Introduction to pointer

➢ To use pointers in C, we must understand below two operators.

▪ 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

▪ In this example print address of variable.


WHAT IS POINTER 1

1 Introduction to pointer
1.1 Introduction to pointer

OUTPUT
CODE

DESCRIPTION

▪ In this example discuss about pointer .


WHAT IS POINTER 1

1 Introduction to pointer
1.1 Introduction to pointer

CODE OUTPUT

DESCRIPTION

▪ In this example print content of pointer and dereference of pointer.


WHAT IS POINTER 1

1 Type of machine
1.2 Type of machine

➢ Type of machine divided to two category


1. Little endian: the LSB byte store first (means at lower address).
2. Big endian: the MSB byte store first (means at lower address).
WHAT IS POINTER 1

1 Type of machine
1.2 Type of machine
CODE OUTPUT

DESCRIPTION

▪ In this example check my machine is little endian and big endian.


WHAT IS POINTER 1

1 Pointer Arithmetic
1.3 Pointer Arithmetic
CODE OUTPUT

DESCRIPTION

▪ In this example used arithmetic pointer in c .


WHAT IS POINTER 1

1 Pointer Arithmetic
1.3 Pointer Arithmetic
CODE OUTPUT

DESCRIPTION

▪ In this example used arithmetic pointer in c .


WHAT IS POINTER 1

1 Passing pointer to function


1.4 Passing pointer to function.

➢ Finally we can summarize function parameters types:


1. Input Parameters (Calling by Value)
The parameter values is completely transmitted to the function. This gives the function the ability to
read the transmitted data only.

2. Input/Output Parameters (Calling by Reference or Pointer)


The parameter pointer (reference) is transmitted only.
This gives the function the ability to read from and write to the original parameters.

3. Output Parameters (Return Value)


The return data of the function is assumed as an output parameter. Normally C does not provide other Output
parameters except the return value.
WHAT IS POINTER 1

1 Passing pointer to function


1.4 Passing pointer to function
CODE OUTPUT

DESCRIPTION

▪ In this example we used call by reference and arguments passed to


function is changed in another function.
WHAT IS POINTER 1

1 Passing pointer to function


1.4 Passing pointer to function
CODE OUTPUT

DESCRIPTION

▪ In this example we used call by value and arguments passed to


function isnt changed in another function.
WHAT IS POINTER 1

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer

▪ 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

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer
CODE OUTPUT

DESCRIPTION

▪ In this example variable x is constant and hack by pointer so put on


stack
WHAT IS POINTER 1

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer
CODE OUTPUT

▪ Run Time Error.

DESCRIPTION

▪ In this example variable x is constant and global and cannot hack


by pointer so put on .rodata
WHAT IS POINTER 1

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer

➢ 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

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer
CODE OUTPUT

DESCRIPTION

▪ In this example ptr is non constant address and constant value.


WHAT IS POINTER 1

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer
CODE OUTPUT

DESCRIPTION

▪ In this example ptr is constant address and non constant value.


WHAT IS POINTER 1

1 What is constant and constant with pointer


1.5 What is constant and constant with pointer
CODE OUTPUT

DESCRIPTION

▪ In this example ptr is constant address and constant value.


WHAT IS POINTER 1

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.

➢ 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

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.

➢ 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

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.
CODE OUTPUT

▪ Run time error

DESCRIPTION

▪ In this example dangling pointer return pointer to integer and


dereference pointer is undefined behavior.
WHAT IS POINTER 1

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.
CODE OUTPUT

▪ No error
WHAT IS POINTER 1

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.
CODE OUTPUT

DESCRIPTION

▪ In this example return pointer to integer and dereference pointer is


compiler error.
WHAT IS POINTER 1

1 Dangling, Void , Null and Wild Pointers


1.6 Dangling, Void , Null and Wild Pointers.
CODE OUTPUT

DESCRIPTION

▪ In this example pointer to void.


WHAT IS POINTER 1

1 Double pointer in c
1.7 Double pointer in c

➢ How to declare a pointer to 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

▪ In this example pointer to pointer.


WHAT IS POINTER 1

1 How to declare a pointer to a function


1.8 How to declare a pointer to a function.

➢ How to declare a pointer to a function?

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

1 How to declare a pointer to a function


1.8 How to declare a pointer to a function
CODE OUTPUT

DESCRIPTION

▪ In this example call function by pointer to function


WHAT IS POINTER 1

1 How to declare a pointer to a function


1.8 How to declare a pointer to a function
CODE OUTPUT

DESCRIPTION

▪ In this example call function by pointer to function


WHAT IS POINTER 1

1 How to declare a pointer to a function


1.8 How to declare a pointer to a function
CODE OUTPUT

DESCRIPTION

▪ In this example call function by pointer to function using array of


pointer to function.
WHAT IS POINTER 1

1 How to declare a pointer to a function


1.8 How to declare a pointer to a function
CODE OUTPUT

DESCRIPTION

▪ In this example pass pointer function to function.


WHAT IS POINTER 1

1 What are near, far and huge pointers


1.9 What are near, far and huge pointers


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.

You might also like