Computer Science/Applications Programming in C

20
M. RAVI KIRAN KUMAR MCA, M.Tech. Govt. Degree College , Gummalakshmipuram Email. Id: [email protected] Computer Science/Applications Programming in C

Transcript of Computer Science/Applications Programming in C

Page 1: Computer Science/Applications Programming in C

M. RAVI KIRAN KUMAR MCA, M.Tech.

Govt. Degree College , Gummalakshmipuram

Email. Id: [email protected]

Computer Science/Applications

Programming in C

Page 2: Computer Science/Applications Programming in C

Objectives

At the end of the session, students should be able to

understand:

- Accessing addresses of 1-D array elements

- Array as a static pointer

- Using a dynamic pointer to access array elements

- Pointers and two-dimensional arrays

- Pointers to pointers and multi-level pointers

- Function pointers

- Benefits and drawbacks of pointers

Page 3: Computer Science/Applications Programming in C

Arrays

- An array is a collection of data of the same data type

organized in successive memory locations.

- Addresses of array elements are in arithmetic progression

and the difference between elements is the size of the data

type.

- Let us take an example and analyze.

Page 4: Computer Science/Applications Programming in C

Array Name - Significance

- What does the name of the array indicate?

- It indicates the starting address of the array i.e. the address

of the first element (indexed at 0).

Page 5: Computer Science/Applications Programming in C

Array – A Static Pointer

- Array name acts a pointer to the base address of the array

- Can be used to navigate through the array

- Cannot be incremented or decremented like a normal

pointer

&a[0] &a[1] &a[2] &a[3] &a[4]

a a+1 a+2 a+3 a+4

1 2 3 4 5

- a = &a[0], a + 1 = &a[1], a + 2 = &a[2], and so on, a + n = &a[n]

- *a = a[0], *(a + 1) = a[1], *(a + 2) = a[2], and so on, *(a + n) = a[n]

- a + n = &a[n]

- a++, a--, a = a – 5 or a = a + 5 are not valid

a+0

Page 6: Computer Science/Applications Programming in C

Using a Dynamic Pointer

- We can use a dynamic pointer to navigate through an array

- Declare a pointer of the same type as the array

- Perform arithmetic operations for navigation a a[1] a[2] a[3] a[4]

1 2 3 4 5

p p p p p

a[0]

Page 7: Computer Science/Applications Programming in C

Pointers – 2-D Arrays

- A 2-D array can be thought of as an array of 1-D arrays

- A 2-D array is stored in row major order i.e. data is stored

row by row

- First element in each row gives the starting address of the

row array.

- In an array a[m][n], a[i] gives the base address of ith row.

The address of jth element is a[i] + j * size.

- Likewise, element a[i][j] has the address a + i * n * size+

j * size where 0 <= i < m and 0 <= j < n

Page 8: Computer Science/Applications Programming in C

Pointers – 2-D Arrays

1 2 3

4 5 6

7 8 9

a[3][3]

Value

1

2

3

4

5

6

7

8

9

Address

0x7ffc0f94fe80

0x7ffc0f94fe84

0x7ffc0f94fe88

0x7ffc0f94fe8c

0x7ffc0f94fe90

0x7ffc0f94fe94

0x7ffc0f94fe98

0x7ffc0f94fe9c

0x7ffc0f94fea0

Row1

Row2

Row3

Example:

a[1][2]

= 0x7ffc0f94fe80(16) + (1 * 3 * 4 + 2 * 4) (10)

= 0x7ffc0f94fe80(16) + 20 (10)

= 0x7ffc0f94fe80(16) + 14 (16)

= 0x7ffc0f94fe94(16)

Page 9: Computer Science/Applications Programming in C

Pointers – 2-D Arrays - Illustration

Page 10: Computer Science/Applications Programming in C

Passing 1D Array Pointers to Functions - Since the name of the array holds the base address of the

array, we can pass it to a pointer along with its size

Page 11: Computer Science/Applications Programming in C

Passing 2D Array Pointers to Functions - 2D array name also indicates its base address but precisely

it is a pointer to an array of pointers.

- So, it needs type casting.

Page 12: Computer Science/Applications Programming in C

Pointers to Pointers - A pointer is also a variable – possesses an address

0x6040 0x1020 1000

var

0x1020

*ptr

0x6040

**ptr_to_ptr

0x9168 int var = 1000;

int *ptr = &var; //Stores the address of variable

int **ptr_to_ptr = &ptr; //Stores the address of pointer

printf (“var = %d\n”, var); //Direct access

printf (“var = %d\n”, *ptr); //Single indirection

printf (“var = %d\n”, **ptr_to_ptr); //Double indirection

- The address of a pointer variable can be stored in another pointer

variable - Called double pointer; Needs double indirection

Page 13: Computer Science/Applications Programming in C

Pointers to Pointers - Example

Page 14: Computer Science/Applications Programming in C

Multi Level Pointers - Pointers can be created in multiple levels

- We need multiple levels of indirection to access the values

stored. - Called triple pointer, quadruple pointer, etc.; Need multiple

indirections

- Difficult to maintain the code

How many levels?

- Logically, there is no limit

- Physically depends on the stack size

- If the stack size is 8 MB and the pointer size is 8 bytes, the number of

pointers is approx. 220. (subject to other pieces of code resident in

memory)

Page 15: Computer Science/Applications Programming in C

Function Pointers - We can also have pointers to functions in C

- A function pointer points to the starting address of the

executable code

- Function pointers cannot be allocated memory dynamically

- The name of a function can itself be used to get address of the

function; The operator & can also be used

- We can have an array of function pointers

- Functions can be passed as arguments to other functions using

function pointers

Page 16: Computer Science/Applications Programming in C

Function Pointers - Example

Page 17: Computer Science/Applications Programming in C

Benefits of Pointers

Pointers optimize the usage of memory space

Faster execution due to manipulation of

addresses

Memory can be dynamically allocated and

released

They are useful in representing complex data

structures

Useful to deal with files

Essential for system programming

Benefits

Page 18: Computer Science/Applications Programming in C

Drawbacks of Pointers

Uninitialized or null pointers cause

segmentation fault

Dynamic memory allocation may cause memory

leaks if not explicitly freed up.

Accessing incorrect memory locations through

pointers may cause software crashes

Pointer bugs are generally difficult to debug

Drawbacks

Page 19: Computer Science/Applications Programming in C

Summary Elements of an array are organized in consecutive memory

locations.

Name of an array represents the base address of the array;

Array name is a static pointer (constant) and cannot be used as

an lvalue.

Arrays can be passed to functions through pointers

Multi-level pointers need multi-level indirection

Pointers to functions can also declared in C

Pointers are useful in system programming

They suffer from memory leaks and segmentation faults

Page 20: Computer Science/Applications Programming in C

References Text Books:

- The C Programming Language – Brian W Kernighan and

Dennis M Ritchie – Second Edition

- Introduction to C Programming – Reema Thareja – Second

Edition

Web Links:

- http://www.toves.org/books/cptr/

- https://gribblelab.org/CBootCamp/8_Pointers.html