Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Dynamic memory allocation
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Pointers in C Rohit Khokher
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 ICS103 Programming in C Lecture 10: Functions II.
Pointers Applications
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 8 Arrays, Strings and Pointers
(Numerical Arrays of Multiple Dimensions)
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
Functions and Pointers
Pointers.
INC 161 , CPE 100 Computer Programming
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Pointers  Week 10.
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to Pointers
Presentation transcript:

Pointers PART - 2

Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection. A pointer variable must be declared before it can be used.

Pointers contd….. Memory can be conceptualized as a linear set of data locations. Variables reference the contents of a locations Pointers have a value of the address of a given location Contents1 Contents11 Contents16 ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 * * * ADDR11 * * ADDR16

Pointers cond…. Examples of pointer declarations: int *a; float *b; char *c; The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.

Example 1 #include int j, k; int *ptr; int main(void) { j = 1; k = 2; ptr = &k; printf("\n"); printf("j has the value %d and is stored at %d\n", j, &j); printf("k has the value %d and is stored at %d\n", k, &k); printf("ptr has the value %p and is stored at %d\n", ptr, &ptr); printf("The value of the integer pointed to by ptr is %d\n", *ptr); return 0; }

& and * Operators & - "address operator" which gives or produces the memory address of a data variable.  int a=3; Now &a will refer to address of a means it will have value * - "dereferencing operator" which provides the contents in the memory location specified by a pointer.  *(&a) will give value at address 3040 i.e a

Pointer Type Pointer is only store memory address but depending upon which type of variable’s address they have will categorize it into four types:  Integer Pointer  Float Pointer  Character Pointer  Double Pointer

Pointer Arithmetic

A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.

Do not try! Addition of two pointers. Multiplication of two pointers with constant. Division of pointer with constant.

Pointer with sizeof This keyword can be used to determine the number of bytes in a data type, a variable, or an array Example: double array [10]; sizeof (double); /* Returns the value 8 */ sizeof (array); /* Returns the value 80 */ sizeof(array)/sizeof(double); /* Returns 10 */

Pointers and Array Array are stored in consecutive memory locations so they can be accessed easily with the help of pointer. With the help of pointer arrays can be passed to function.

Example 2 #include int my_array[] = {1,23,17,4,-5,100}; int *ptr; void main(void) { int i; ptr = &my_array[0]; /* point our pointer to the first element of the array */ for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); }

Example 3: Passing Array to function

2D Array or Matrix Example: Storing Roll No. & Marks together #include void main( ) { int stud[4][2], i; for (i=0; i<=3; i++) { printf("\n Enter roll no. and marks of student"); scanf ("%d %d",&stud[i][0],&stud[i][1]); } for( i=0; i<=3; i++) printf("\n%d %d",stud[i][0],stud[i][1]); }

2-D Array Consider a 2D array declared as Memory Map of 2D array

Pointers and 2D Array #include void main( ) { int s[4][2]={{1234,56},{1212,33},{1434,80},{1312,78}}, i; for (i=0;i<=3;i++) printf("\nAddress of %d th 1-D array = %u",i,s[i]); }

2D Array with Pointers #include void main( ) { int s[4][2]={{1234,56},{1212,33},{1434,80},{1312,78}}, i, j; for(i=0; i<=3; i++) {printf("\n"); for(j=0; j<=1; j++) printf("%d ",*(*(s+i)+j)); }

Pointers & Function Pointers can be used to pass addresses of variables to call functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as "call by value ".

Pointer and Function If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.

#include void swap ( int a, int b ) ; void main ( ) { int a = 5, b = 6; swap (a, b) ; printf(“ Within Main function”); printf("a=%d b=%d\n",a,b) ; } void swap( int a, int b ) { int temp; temp= a; a= b; b = temp ; printf(“ Within Swap Function”); printf ("a=%d b=%d\n", a, b); } Call by Value Function

#include void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; swap (&a, &b) ; printf(“Within Main Function”); printf("a=%d b=%d\n",a,b) ; return 0 ; } void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf(“Within Swap Function”); printf ("a=%d b=%d\n", *a, *b); } Call by Reference Function

Assignment Compare call by value and Cal by reference with the help of one example. Define an array in main method and print it using a function by passing reference of array to the function.