POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
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.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© 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.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Variables and memory addresses
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
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.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Computer science C programming language lesson 3.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Lecture 5 Pointers 1. Variable, memory location, address, value
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Functions and Pointers
Hassan Khosravi / Geoffrey Tien
Functions and Pointers
Chapter 5 POINTERs.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Programming in C Pointer Basics.
Programming in C Pointer Basics.
5th Chapter Pointers in C++.
CSCE 206 Lab Structured Programming in C
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
Programming in C Pointer Basics.
CSCE 206 Lab Structured Programming in C
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions

Introduction to Pointer  Variables are stored in the memory  Every variable has unique memory location(Address)  Address can be accessed using ampersand (&) operator

Program to display the address of the variable #include void main () { int var1; printf("Address of var1 variable: %x\n", &var1 ); } Address of var1 variable: bff5a400

Pointers  A pointer is a variable whose value is the address of another variable  You must declare a pointer before you can use it to store any variable address  Declaring Pointer  Syntax type *var-name;  Eg : int *p1;

Program to access the memory location using pointer #include void main () { int var = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &var; /* store address of var in pointer variable*/ printf("Address of var variable: %x\n", &var ); printf("Address stored in ip variable: %x\n", ip ); printf("Value of *ip variable: %d\n", *ip ); } Address of var variable: Address stored in ip variable: Value of *ip variable: 20 Address of var variable: Address stored in ip variable: Value of *ip variable: 20

Program Explanation Memory 20 var int var = 20; 20 var int *ip; ip ip = &var; ip 65558

Other pointer types int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */

Null Pointer in C  A pointer that is assigned NULL is called a null pointer.  It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned.  The NULL pointer is a constant with a value of zero

Null Pointer – Example Program #include void main () { int *ptr = NULL; printf("The value of ptr is : %x\n", ptr ); } The value of ptr is 0

Guess the output of the following program # include void main() { int *p, x=20; p=&x; x= *p+15; printf(“%d”, x); printf(“%d”, *p); }

Pointer arithmetic  int *a;  Increment Operator  a++ : address will be incremented by 2 (if it is int)  (*a)++ : content will be incremented by 1  Example  int *a, b=30; /* assume address of b is 2000 */  a=&b;  b++ => 2002  *b++ => 31

Pointer arithmetic with Array – Example1 #include void main () { int var[ ] = {10, 100, 200}; int i, *ptr; ptr = &var[0]; printf(“the value is %d\n”, *ptr); ptr++; printf(“the value is %d\n”, *ptr); printf(“the address is %x\n”, ptr); } Assume starting address of var[] is 2500 the value is 10 the value is 100 the address is 2502 the value is 10 the value is 100 the address is 2502

Pointer arithmetic with array – Example2 #include void main () { int var[ ] = {10, 100, 200}; int i, *ptr; ptr = &var[0]; printf(“the value is %d\n”, *ptr); (*ptr)++; printf(“the value is %d\n”, *ptr); printf(“the address is %x\n”, ptr); } Assume starting address of var[] is 2500 the value is 10 the value is 11 the address is 2500 the value is 10 the value is 11 the address is 2500

Sum of array using pointer – Example 3 #include void main() { int a[5]={1,2,3,4,5}; int i,sum=0; int *b; b=a; for(i=0;i<5;i++) { sum=sum + *b; b++; } printf("\n The Sum is %d",sum); } The Sum is 15

Pointer and Functions  Call by value  Call by Reference  Returning pointers

Call by value # include int add(int, int); void main() { int no1,no2, sum; printf(“Enter two numbers\t”); scanf(“%d%d”,&no1,&no2); sum=add(no1,no2); printf(“the sum is %d \t”, sum); } int add(int x, int y) { int s; s=x+y; return(s); } Call by Value

Call by Reference – Example(swapping of two numbers) # include void swap(int*, int*); void main() { int no1=5, no2=8; printf(“values before swapping %d\t %d\n”, no1,no2); swap(&no1,&no2); printf(“values after swapping %d\t %d”, no1,no2); } void swap(int *x, int *y) { int t; t= *x; *x=*y; *y=t; } Call by Reference values before swapping 58 values after swapping 85 values before swapping 58 values after swapping 85

Dynamic memory allocation  Static Memory Allocation  Allocating memory to variable during compile time of your program  Eg. : int a[20];  Disadvantage : Wastage of Memory Space  Dynamic Memory allocation  Allocating memory to variables during run time of your program  Advantage : No wastage of Memory Space

Allocating memory during run time # include void main() { int n, *a,k=1; printf(“Enter no. of values “); scanf(“%d”, &n); a= (int *)malloc(n*sizeof(int)); /* malloc(n*2) */ for(i=0;i<n;i++) { *a=k; a++; k++; }