POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

An introduction to pointers in c
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointers *, &, array similarities, functions, sizeof.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
UNIT 5 C Pointers.
Functions and Pointers
Standard Version of Starting Out with C++, 4th Edition
Lecture 6 C++ Programming
Dynamic Memory Allocation
Pointers.
Functions and Pointers
14th September IIT Kanpur
Object Oriented Programming COP3330 / CGS5409
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

POINTER Prepared by MMD, Edited by MSY1

 Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic variables Prepared by MMD, Edited by MSY2

 In previous chapter, you have learnt about how a function could return a single value under its name (passed by value).  But, for a function that must return multiple values we use parameter passing by pointers. (It is so called as passed by reference).  A pointer is a variable which directly points to a memory address.  It will allow the programmer to directly manipulate the data in memory.  So far, we have seen that a variable is used to store a value. A pointer variable, however, does not store a value but instead store the address of the memory space which contain the value. Prepared by MMD, Edited by MSY3

 Why would we want to use pointers? ◦ To call a function by reference so that the data passed to the function can be changed inside the function. ◦ To create a dynamic data structure which can grow larger or smaller as necessary. Prepared by MMD, Edited by MSY4

 A variable declaration such as, ◦ int number = 20; causes the compiler to allocate a memory location for the variable number and store in it the integer value 20. ◦ This absolute address of the memory location is readily available to our program during the run time. ◦ The computer uses this address to access its content. Prepared by MMD, Edited by MSY5 number 20 number directly references a variable whose value is 20

 A pointer declaration such as,  int *numberptr, number = 20; declares numberptr as a variable that points to an integer variable. Its content is not integer value 20 but, it is a memory address.  The * indicates that the variable being defined is a pointer.  In this sense, a variable named directly references a value, however a pointer indirectly references a value. Prepared by MMD, Edited by MSY6 number 20 numberptr indirectly references a variable whose value is 20 numberptr

Prepared by MMD, Edited by MSY7 Memory contentMemory address 0x0000 0x0001 0x0002 0x0003 …... 0xFFFF 0xFFFE 0xFFFD 0xFFFC x A variable of type int is stored here. The address of this location is 0x0002. This location contains the value 0x0002. Therefore this location stores a pointer variable which points to the address 0x0002.

 General format: data_type *ptr_name;  Example: ◦ Pointer to an int: int *intptr; ◦ Pointer to a char: char *charptr;  The asterisk (*) character in front of the variable name tells that the variable is a pointer and not a variable to store value. Prepared by MMD, Edited by MSY8

 To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0 or NULL before being used.  When a pointer is created, it is not pointing to any valid memory address. Therefore, we need to assign it to a variable’s address by using the & operator. This operator is called a reference operator.  After a pointer is assigned to a particular address, the value in the pointed address can be accessed using the * operator. This operator is called a dereference operator. Prepared by MMD, Edited by MSY9

 Look at this example: int num = 9; int *num_ptr; num_ptr = # printf(“num = %d”, *num_ptr);  Output: num = 9 Prepared by MMD, Edited by MSY10

Prepared by MMD, Edited by MSY11 numPtr num 9 Graphical representation of a pointer pointing to an integer variable in memory numPtrnum Representation of num and numPtr in memory

Prepared by MMD, Edited by MSY12 #include void main(void) { int var = 10; int *ptrvar = &var; printf(“The address of the variable var is: %x\n”, &var); printf(“The value of the pointer ptrvar is: %x\n”, ptrvar); printf(“Both values are the same\n”); printf(“The value of the variable var is: %d\n”, var); printf(“The value of *ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same\n”); printf(“The address of the value pointed by ptrvar is: %d\n”, &*ptrvar); printf(“The value inside the address of ptrvar is: %d\n”, *&ptrvar); printf(“Both values are the same\n”); }

/*Sample Output The address of the variable var is: The value of the pointer ptrvar is: Both values are the same The value of the variable var is: 10 The value of *ptrvar is: 10 Both values are the same The address of the value pointed by ptrvar is: The value inside the address of ptrvar is: Both values are the same Press any key to continue */ Prepared by MMD, Edited by MSY13

 A function call by reference can be done by passing a pointer to the function argument (the same way as passing a normal variable to the argument).  When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change.  Therefore, we can pass the result of the function through the function argument without having to use the return statement. In fact, by passing pointers to the function argument, we can return more than one value. Prepared by MMD, Edited by MSY14

 When a pointer is passed to a function, we are actually passing the address of a variable to the function.  Since we have the address, we can directly manipulate the data in the address.  In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable. Prepared by MMD, Edited by MSY15

 To pass the value of variable by pointers between two functions, we must do the following: 1.Declare the variable that is meant to return a value to the calling function as a pointer variable in the formal parameter list of the function. void called_function(int *result); 2.When to call the function, use a variable together with address operator (&) called_function(&value_returned); Prepared by MMD, Edited by MSY16

 Note : the effect of the above two actions is the same as the effect of the declarations  int value_returned;  int *result=&value_returned; 3.Declare the function with pointer parameter appropriately in a function prototype by using symbol * as a prefix for pointer parameters. void called_function(int *x); Prepared by MMD, Edited by MSY17

Prepared by MMD, Edited by MSY18 void Func1(int a, int b) { a = 0; b = 0; printf(“Inside Func1, a = %d, b = %d\n”, a, b); } #include void Func1(int, int); void Func2(int *, int *); void main(void) { int a = 8, b = 9; printf(“Before Func1 is called, a = %d, b = %d\n”, a, b); Func1(a, b); printf(“After Func1 is called, a = %d, b = %d\n”, a, b); printf(“\nBefore Func2 is called, a = %d, b = %d\n”, a, b); Func2(&a, &b); printf(“After Func2 is called, a = %d, b = %\nd”, a, b); }

Prepared by MMD, Edited by MSY19 Output: Before Func1 is called, a = 8, b = 9 Inside Func1, a = 0, b = 0 After Func1 is called, a = 8, b = 9 Before Func2 is called, a = 8, b = 9 Inside Func2, *pa = 0, *pb = 0 After Func2 is called, a = 0, b = 0 void Func2(int *pa, int *pb) { *pa = 0; *pb = 0; printf(“Inside Func2, *pa = %d, *pb = %d\n”, *pa, *pb); }

 Variables that can be created and destroyed during program execution.  To use dynamic variables: ◦ Decide the type of data to be stored in such variable ◦ Declare a pointer variable of that data type ◦ Assign to the pointer the address created by the standard library function malloc. (malloc is declared in the standard header file stdlib.h) Prepared by MMD, Edited by MSY20

 The function malloc is used to allocate a certain amount of memory during the execution of a program.  The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory.  When the amount of memory is not needed anymore, we must return it to the operating system by calling the function free. Prepared by MMD, Edited by MSY21

Example: To create a dynamic variable of type int: 1. Declare a pointer variable of type int int *ptr; 2. Use the function malloc to create the dynamic variable ptr =malloc(sizeof(int));  The argument of malloc function is the number of bytes to be allocated for the dynamic variable it creates. Prepared by MMD, Edited by MSY22

 Normally we use the sizeof operator on a data type to specify the number of bytes as the argument of malloc.  malloc will allocate from the free memory location that can store a value in the indicated number of bytes.  In this example, malloc instructs the OS to allocate a memory location of sizeof(int) bytes.  If there is enough space in the free store, the OS gives it to the program and makes its address available to malloc i.e malloc returns the address of the dynamically allocated memory location to the pointer variable ptr.  If there is no available space, malloc returns NULL. Prepared by MMD, Edited by MSY23

 We may destroy the dynamic variable by using free function.  free(ptr) causes the OS to release the memory location pointed by the pointer variable ptr.  However, the memory location for the variable ptr is not deallocated and its content is not changed (it still contains an address but can no longer be used to access the memory legally)  It is advisable to assign the value NULL to ptr immediately after the application of the free function. Prepared by MMD, Edited by MSY24

int *ptr; ptr = malloc(sizeof(int)); *ptr = 5; free(ptr); ptr=NULL; Prepared by MMD, Edited by MSY25 ? ptr ? ptr 5 ptr Invalid address ptr NULL

Prepared by MMD, Edited by MSY26 /* * $Id: sizeof.c,v /07/05 10:37:54 sms Exp $ * * * Program to display data sizes. */ #include #define printsize(x) printf ("sizeof (" #x ") = %d\n", sizeof (x)) main () { printf ("\nNormal printf\n"); printsize (char); printsize (double); printsize (float); printsize (int); printsize (long); printsize (long long); printsize (long double); printsize (short); printsize (void *); printsize (int *); }

Normal printf sizeof (char) = 1 sizeof (double) = 8 sizeof (float) = 4 sizeof (int) = 4 sizeof (long) = 4 sizeof (long long) = 8 sizeof (long double) = 8 sizeof (short) = 2 sizeof (void *) = 4 sizeof (int *) = 4 Prepared by MMD, Edited by MSY27

FunctionTask mallocAllocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space callocAllocates space for an array of elements initializes them to zero and returns a pointer to the memory freeFrees previously allocated space reallocModifies the size of previously allocated space Prepared by MMD, Edited by MSY28

 This chapter has exposed you about:  Pointer variables and parameter passing by reference/ pointers  The usage of symbol * and address operator (&)  Passing by pointers allows us to return multiple values from functions  How to use dynamic variables Prepared by MMD, Edited by MSY29

 %c Displays char data  %i, %d Display int data  %f Displays float data  %2f Displays double data  %.2f Displays float with specified decimal (after.)  %s Displays string data  %o Displays octal data  %x Displays hexadecimal data Prepared by MMD, Edited by MSY30