POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Introduction to C Programming
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
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.
Kernighan/Ritchie: Kelley/Pohl:
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.
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.
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.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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:
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
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 and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Types of Operator Arithmetic operator Relational operator Logical operator Assignment operator Increment or decrement operator(unary) Bitwise operator.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
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.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
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.
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
Windows Programming Lecture 03. Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Array in C# Array in C# RIHS Arshad Khan
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions and Pointers
Visit for more Learning Resources
Module 2 Arrays and strings – example programs.
Programming Languages and Paradigms
Pointers.
Functions and Pointers
Buy book Online -
Chapter 5 POINTERs.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
POINTER CONCEPT 4/15/2019.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Chapter 9: Pointers and String
Chapter 5 POINTERs Visit to more Learning Resources.
POINTER CONCEPT 8/3/2019.
Arrays and Pointers.
Presentation transcript:

POINTERS

1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following are valid y = *p1 * *p1; same as (*p1)*(*p2) sum=sum + *p1; z = 5* - *p2/ *p1; same as (5 * (-(*p2)))/(*p1) p1++ -p2 sum += *p2

Pointers can also be compared using relational operators. p1 > p2, p1 == p2, p1 != p2 Pointers cannot be divided or multiplied or added p1/p2 or p1*p2 or p1/3 or p1+p2 Add integer to or subtract integers from pointers are allowed Subtraction of one pointer from another of same type is allowed

Important note *ptr++ is equivalent to *(ptr++) as ++ has greater operator precedence expression will increase the value of ptr so that it points to the next element. (*ptr)++ will increment the value of the variable whose address is stored in ptr.

1.b) POINTER INCREMENT P1++ or p1=p1+1 will cause the pointer to point to the next value of its type. e.g if p1 value is 2800 then after p1++ it will be *p1 will give value stored at next location The value by which it is increased is called as scale factor Sizeof(variable) – operator returns size of bytes occupied by that variable

#include int main() { int num, *pnum; pnum=# *pnum=10; printf("*pnum= %d", *pnum); printf(" num = %d", num); *pnum= *pnum + 1; //increments the value of num printf("\n after increment *pnum=%d", *pnum); printf("\n after increment num=%d", num); getch(); return 0; } Find out output.

1.d) POINTERS AND CHARACTER STRINGS String without pointer char str[5]=“good” String with pointer char *str=“good”; pointer str now points to the first character of the string or char *string1; string1= “good”; string1 is not a string it is pointer which also will work as a name for the string To print string string1 we can use printf or puts (). * not required

/* STRING HANDLING USING POINTERS */ void main() { char *name; int length; char *cptr; name = "DELHI"; cptr=name; printf ("%s\n", name); while(*cptr != '\0') { printf("%c is stored at address %u\n", *cptr, cptr); cptr++; } length = cptr - name; printf("\nLength of the string = %d\n", length); getch(); } Output:- DELHI D is stored at address 170 E is stored at address 171 L is stored at address 172 H is stored at address 173 I is stored at address 174 Length of the string = 5

#include int main() { // Declaring/Initializing three characters pointers char *ptr1 = "Himanshu"; char *ptr2 = "Arora"; char *ptr3 = "TheGeekStuff"; //Declaring an array of 3 char pointers char* arr[3]; // Initializing the array with values arr[0] = ptr1; arr[1] = ptr2; arr[2] = ptr3; //Printing the values stored in array printf("\n [%s]\n", arr[0]); printf("\n [%s]\n", arr[1]); printf("\n [%s]\n", arr[2]); getchar(); return 0; }

1.c) POINTERS AND 1D- ARRAYS Accessing array elements by pointers is always faster than accessing them by subscripts. e.g int x[5] = { 1,2,3,4,5}; Int *p; We can make pointer p point to an array x by: p = x or p = &x[0] 1D, x[i] can be represented by *(x+i) or *(p+i)

Array notation is a form of pointer notation. The name of the array is the starting address of the array in memory. It is also known as a base address. In other words, base address is the address of the first element in the array or the address of arr[0]. Int *ptr; Ptr = &arr[0]; Here, ptr is made to point to the first element of the array. Ptr++; // will point to the second element of the array

Impt : Num[i], *(num+i), *(i+num), i[num] -- all refer to same element int main() { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i ; for ( i = 0 ; i <= 5 ; i++ ) { printf ( "\naddress = %u ", &num[i] ) ; printf ( "element = %d %d ", num[i], *( num + i ) ) ; printf ( "%d %d", *( i + num ), i[num] ) ; } getch(); return 0; } This output is on 64-bit pc due to which int is taking 4 byte

int main() { int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int i, *j ; j = &num[0] ; /* assign address of zeroth element */ for ( i = 0 ; i <= 5 ; i++ ) { printf ( "\naddress = %u ", j ) ; printf ( "element = %d", *j ) ; j++ ; /* increment pointer to point to next location */ } getch(); return 0; } This output is on 64-bit pc due to which int is taking 4 byte

E.g main() { int b[]={ 10,20,30,40,50}; int i, *k; k=b; for(i=0;i<=4;i++) { Printf(“\n%d”, *k); k++; } what will be output ??????

1.d) POINTERS AND 2D ARRAY Int b[3][3] = { {1,3,5}, {7,9,11}, {13,15,17}} b is a pointer to b[0][0] b[0] is also a pointer to b[0][0] b[1] is a pointer to b[1][0] b[2] is a pointer to b[2][0] *b is a pointer to b[0] (special case) *b[1] is the value of b[1][0] which is 7 *b[2]+1 is the value of b[2][0]+1 (which is 14)

Int b[10]; we can write b[0] as *(b+0) or *b Int a[0][0] can also be written as *(a[0]+0) or *(*a+0) or **a S[2][1] *(s[2]+1) *(*(s+2)+1) all refer to same element Fig. memory map of a 2D array

OUTPUT

Exercise : find out the output for the program and explain working of program

1.e) POINTERS AND 3D ARRAY Arr[2][3][1] is written as *(*(*arr+2)+3)+1)

1.f) ARRAY OF POINTERS Like array of ints or an array of floats, we have array of pointers Array of pointers is a collection of addresses. The addresses present in the array of pointers can be : a) addresses of isolated variables b) addresses of array element c) or any other addresses

Example 1 : Array of pointer

Example 2 : Array of pointers // run this code on software Output ????????? Find by yourself

Questions) using pointers WAP to find largest number in an array using pointer WAP to find smallest number in an array using pointer WAP to exchange the values of largest with the smallest number of an array WAP to find the sum of all elements of an array WAP to read and display the values of 3*3 matrix WAP to read and display the values of 3*3*3 matrix

1.f) POINTER AND STRUCTURE Pointer can also point to a structure. Such pointers are called as ‘structure pointers’ Structure variables can be accessed by ‘->’ operator or dot (.) operator Pointer_var->structure_var (*pointer_var).structure_var

Example : structure pointer