Tejalal Choudhary “C Programming from Scratch” Pointers

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

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.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
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:
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Khalid Rasheed Shaikh Computer Programming Theory 1.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
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
Pointers What is the data type of pointer variables?
CS1010 Programming Methodology
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
BILASPUR UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
LESSON 3 IO, Variables and Operators
Arrays Declarations CSCI N305
Pointers and Pointer-Based Strings
Visit for more Learning Resources
INC 161 , CPE 100 Computer Programming
POINTERS.
Pointers.
Basic notes on pointers in C
Visit for more Learning Resources
Buy book Online -
Computer Science 210 Computer Organization
Buy book Online -
Tejalal Choudhary “C Programming from Scratch” Function & its types
Chapter 5 POINTERs.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Tejalal Choudhary “Computer Programming” Fundamentals of “C”
Pointers  Week 10.
Windows Programming Lecture 02.
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
Pointers Call-by-Reference CSCI 230
Outline Defining and using Pointers Operations on pointers
5.1 Introduction Pointers Powerful, but difficult to master
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointer Operations.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
C ( Programming Language ) PSK Technologies By: Arti Sontakke An ISO 9001:2015 (QMS) Certified IT Company Computer Education | Software Development | Computer.
C Programming Pointers
Chapter 9: Pointers and String
CSCE 206 Lab Structured Programming in C
Course Outcomes of Programming In C (PIC) (17212, C203):
Chapter 5 POINTERs Visit to more Learning Resources.
Storage Classes.
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

Tejalal Choudhary “C Programming from Scratch” Pointers Department of Computer Science & Engg SDBCT, Indore tejalal.choudhary@sdbct.ac.in

Contents What is pointer Printing values using pointer Call by reference Pointer arithmetic array and pointer Structure and pointer

Int a=10; Int b=4; char ch=‘A’ 1 2 3 4 5 6 7 12 13 18 24 19 a … … 00000000 00001010 65529 65524 00000000 00000100 65535 65530 00100000 b ch

Recalling variable What does above declaration mean? int a=10; a is an integer variable Reserves 2 bytes in memory Associate the name a with memory location a will have some address

Address of Operator(&) The & operator is used to access the address of a variable The %u is used to print the address with & int a=10; Following statement will print the address of variable a printf(“Address of a=%u”, &a);

Value at address operator(*) Gives the value stored at a particular address Also known as indirection operator. int a=10; The following statements will print the value of a printf(“Value of a=%d”, a); //10 printf(“Value of a=%d”, *(&a)); //10

What is a pointer ? Declaration: int *p; Pointers are variables that contain addresses Pointers would always contain whole numbers. A pointer when incremented always points to an immediately next location of its type. A pointer is a variable which holds/stores the address of another variable Declaration: int *p; p is a pointer variable which can store address of any integer variable Pointer variable can not store normal values p=3456; // not allowed P=&a; //allowed if a is an integer variable

int i = 3; int *j; int **k ; j=&i; K=&j;

} j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ; int i = 3, *j, **k ; j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ; printf ( "\nAddress of i = %u", j ) ; printf ( "\nAddress of i = %u", *k ) ; printf ( "\nAddress of j = %u", &j ) ; printf ( "\nAddress of j = %u", k ) ; printf ( "\nAddress of k = %u", &k ) ; printf ( "\nValue of j = %u", j ) ; printf ( "\nValue of k = %u", k ) ; printf ( "\nValue of i = %d", i ) ; printf ( "\nValue of i = %d", * ( &i ) ) ; printf ( "\nValue of i = %d", *j ) ; printf ( "\nValue of i = %d", **k ) ; } 65524 65524 65524 65522 65522 65520 65524 65522 3 3 3 3

Call by reference s=sum(&a, &b); Arguments can be passed to functions in one of the two ways: Sending the values of the arguments Sending the addresses of the arguments int a=10, b=20, s; s=sum(&a, &b); Function definition int sum(int *p, int *q) { int r=*p+*q; return r; } Passing the addresses of a & b We are passing addresses, we need pointer variable. p & q are pointer

Pointer arithmetic Addition of two pointers Don’t attempt following operations on pointer Addition of two pointers Multiplication of a pointer with a constant Division of a pointer with a constant All of the above

What will be the output of the program? void main(){  int i = 3, *x ; float j = 1.5, *y ; clrscr(); x = &i ; y = &j ; //suppose address of i=200 and address of j=400 x++ ; y++ ; printf( "\n %u ", x ) ; printf( " %u", y ) ;  getch(); }  202 402 200 400 204 408 202 404 202 404

What will be the output of the program?   #include<stdio.h> #include<conio.h> void main( ) { int i = 3, *j, **k ; clrscr(); j = &i ;//suppose address of i is 200 k = &j ;//suppose address of j is 400 printf ( "\n%u", j ) ; printf ( ",%u", k ) ; printf ( ",%d", * ( &i ) ) ; printf ( ",%d", **k ) ; getch(); } 200 400 202 400 200 3 400 3 400 3 200 3 200 400 3 3 200 400 3 3