Chapter 5 POINTERs Visit to more Learning Resources.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
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:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
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.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Khalid Rasheed Shaikh Computer Programming Theory 1.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
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]
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
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.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
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 and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
Lecture 5 Pointers 1. Variable, memory location, address, value
Pointers What is the data type of pointer variables?
Pointers verses Variables
CHP-2 ARRAYS.
LESSON 3 IO, Variables and Operators
Pointers and Pointer-Based Strings
Visit for more Learning Resources
CNG 140 C Programming (Lecture set 10)
Module 2 Arrays and strings – example programs.
Tejalal Choudhary “C Programming from Scratch” Pointers
Visit for more Learning Resources
Buy book Online -
C Passing arrays to a Function
Chapter 5 POINTERs.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
EKT150 : Computer Programming
Dynamic Memory Allocation
Lecture 18 Arrays and Pointer Arithmetic
Dynamic Memory Allocation
Introduction To Programming Information Technology , 1’st Semester
Outline Defining and using Pointers Operations on pointers
5th Chapter Pointers in C++.
Array as arguments, Multidimensional arrays
Chapter 2 Array and String Visit to more Learning Resources.
Chapter 16 Pointers and Arrays
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers and Pointer-Based Strings
POINTER CONCEPT 4/15/2019.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Chapter 9: Pointers and String
Course Outcomes of Programming In C (PIC) (17212, C203):
POINTER CONCEPT 8/3/2019.
C Programming Mr. KAJAL MAJI ASSISTANT PROFESSOR DEPARTMENT OF PHYSICS
Introduction to Pointers
Introduction to Pointers
Visit for more Learning Resources
Presentation transcript:

Chapter 5 POINTERs Visit to more Learning Resources

POINTERS What is a pointer ? A pointer is nothing but a variable that contains the address which is a location of the another variable in memory. Benefits of using the pointer : A pointer enables us to access a variable that is defined outside the function. Pointer are more efficient in handling data table. Pointer reduces the length and complexity of the program. The use of pointer array to character string results in saving of data storage space in memory.

Declaring and initializing pointers : syntax : data_type *pt_name ; This tells the compiler three things about the variable pt_name. 1. * tells the compiler that the variable pt_name is a pointer variable. 2. pt_name needs a memory location. 3. pt_name points to a variable of type data_type.

Ex: int quantity=179 ; int *p; p=&quantity; quantity variable 179 value 5000 Address Note : you can know the address of variable using %u format specification. You can’t assign an absolute address to a pointer variable directly. Ex: p = 5000; It is not possible

Pointers expression : void main() { int a,b,*p1,*p2, x, y, z ; a =12; b = 4; p1=&a; p2 = &b; x = *p1 * *p2 – 6; printf(“Address of a = %u\n”,p1); printf(“Address of b=%u\n”,p2); printf(“\n”); printf(“a=%d, b=%d\n”,a , b); printf(“x=%d \n”,x);

Pointer increments and scale factor : p1++ will cause the pointer p1 points to the next value of its type. Ex :- if p1 is an integer pointer with the initial value say 2800,then after the operations p1=p1+1,the value of p1 will be 2802,not 2801 when we increment pointer its value is increased by the length of the data type that it points to. This length is called scale factor.

Pointer and Arrays :- When an array is declared, the compiler allocates a base Address and sufficient amount of storage to contains all The elements of the array in memory location. Base address is the location of the first element of the array int x[5] = {1,2,3,4,5} Element x[0] x[1] x[2] x[3] x[4] Value 1 2 3 4 5 Address 1000 1002 1004 1006 1008 So, base address is, x = &x[0] = 1000

If we declare p as integer pointer, then we can make the pointer p to point to the array x by the following statements. p = x or p = &x[0] Now we can access every element of x using p++ to move from one element to another. p = &x[0] (=1000) p+1 = &x[1] (=1002) p+2 = &x[2] (=1004) p+3 = &x[3] (=1006) p+4 = &x[4] (=1008)

/*Program of sum n array elements*/ #include<stdio.h> main() { int x[10],i,*p,n,sum=0; printf("enter the N:"); scanf("%d",&n); printf("enter the the data:\n"); for(i=0;i<n;i++) scanf("%d",&x[i]); p = x;

2,0 2,3 Array using pointer notation P P + 1 p+2 *(p+2) + 3 *(p+2) 0 1 2 3 4 2,0 2,3 P 1 P + 1 p+2 2 3 *(p+2) *(p+2) + 3 p = pointer to first row p+i = pointer to ith row *(p+i) = pointer to first element in the ith row *(p+i)+j = pointer to jth elements *(*(p+i)+j)=value stored in the cell(i,j)

/* Program of sum of n array elements*/ #include<stdio.h> void main() { int x[10][10],i,j,n,sum=0; clrscr(); printf("enter the N:"); scanf("%d",&n); printf("enter the the data:\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&x[i][j]);

for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("\n%d",*(*(x+i)+j)); sum=sum + *(*(x+i)+j); } printf("\nsum=%d",sum); getch();

Pointer and character string :- In c, a constant character string always represents a pointer to that string. And therefore you can directly write char *name; name = “DELHI” But remember that this type of assignment does not apply on character arrays. char name[20];

char *name[3] = {“New Zealand”,“Australia”,“India”} Where name to be an array of three pointers to a character, each pointer points to a particular name. name[0]=“New Zealand”, name[1]=“Australia”, name[2]=“India” This declaration allocates only 28 bytes. The character arrays with the rows of varying length are called ragged array.

/****PROGRAMM FOR STRING PRINT USING POINTER ****/ #include<stdio.h> void main() { char name[10],*ptr; clrscr(); printf("enter the name => "); scanf("%s",name); ptr=name; printf("\n");

char *name ; name = “Delhi” while(*ptr != '\0') { printf("%c",*ptr); ptr++; } getch(); Note : if we want to directly assign string constant then we have to make name as character pointer. char *name ; name = “Delhi” For more Details contact us