Pointers  Week 10.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
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 A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Introduction to C Programming CE
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
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()
 2007 Pearson Education, Inc. All rights reserved C Pointers.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Dale Roberts, Lecturer Computer Science, IUPUI.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Functions and Pointers
Chapter 7 - Pointers Outline 7.1 Introduction
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Tejalal Choudhary “C Programming from Scratch” Pointers
14th September IIT Kanpur
Buy book Online -
Systems Programming Concepts
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Programming in C Pointer Basics.
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
5.1 Introduction Pointers Powerful, but difficult to master
CS111 Computer Programming
7. Pointers, Dynamic Memory
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
CS1100 Computational Engineering
CSCE 206 Lab Structured Programming in C
CISC181 Introduction to Computer Science Dr
C Pointers Systems Programming.
Pointer Arithmetic By Anand George.
pointer-to-pointer (double pointer)
Pointer Syntax Review *ptrName Declaring Pointers
Introduction to Pointers
Presentation transcript:

Pointers  Week 10

Pointers Pointer is a variable just like other variables of c but only difference is unlike the other variable it stores the memory address of any other variables of c. This variable may be type of int, char, array, structure, function or any other pointers.

Pointer Variable Declarations Pointer declarations ‘*’ used with pointer variables int *myPtr; float *Ptr; Char *strPtr; Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2; Can declare pointers to any data type

Pointers int a=5; int * ptr; ptr=&a; About variable a: About variable ptr: 1. Name of variable : a 4. Name of variable : ptr 2. Value of variable which it keeps: 5 5. Value of variable which it keeps: 1025 3. Address where it has stored in memory : 1025 (assume) 6. Address where it has stored in memory : 5000 (assume)

Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer * operator Used as nickname for variable inside of function void double( int *number ) { *number = 2 * ( *number ); }

Inside cubeByReference, *nPtr is used (*nPtr is number). 1 /* Example (2) 2 Cube a variable using call-by-reference 3 with a pointer argument */ 4 5 #include <stdio.h> 6 7 void cubeByReference( int * ); /* prototype */ 8 9 int main() 10 { 11 int number = 5; 12 13 printf( "The original value of number is %d", number ); 14 cubeByReference( &number ); 15 printf( "\nThe new value of number is %d\n", number ); 16 17 return 0; 18 } 19 20 void cubeByReference( int *nPtr ) 21 { 22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */ 23 } Notice that the function prototype takes a pointer to an integer (int *). Function prototype 1 Initialize variables 2. Call function 3. Define function Program Output Notice how the address of number is given - cubeByReference expects a pointer (an address of a variable). Inside cubeByReference, *nPtr is used (*nPtr is number). The original value of number is 5 The new value of number is 125

Arithmetic operation with pointer #include<stdio.h> int main() { int *ptr=( int *)1000; ptr=ptr+1; { double *p=(double *)1000; p=p+3; printf(" %d",ptr); return 0; printf(" %d",p); return 0; } Output: 1004 } Output: 1024

Arithmetic operation with pointer #include<stdio.h> int main() { int *p=(int *)1000; int *temp; temp=p; p=p+2; printf("%d %d\n",temp,p); printf("difference= %d",p-temp); return 0; } Output: 1000 1008 Difference= 2 { float *p=(float *)1000; float *q=(float *)2000; printf("Difference= %d",q-p); return 0; Output: Difference= 250

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

Pointers Power Pointers to Functions Pointers to Structure Pointers to …… Pointers