Windows Programming Lecture 03. Pointers and Arrays.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
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.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Pointers Applications
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2006 Pearson Education, Inc. All rights reserved Arrays.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
POINTERS.
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.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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 It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
7 Arrays.
Windows Programming Lecture 02.
MSIS 655 Advanced Business Applications Programming
Homework Starting K&R Chapter 5 Good tutorial on pointers
7 Arrays.
Pointer Arithmetic By Anand George.
Review of Java Fundamentals
Introduction to Pointers
Presentation transcript:

Windows Programming Lecture 03

Pointers and Arrays

Arrays An array is a collection of variables of the same type. Individual array elements are identified by an integer index. In C the index begins at zero and is always written inside square brackets.

According to C language specification, –Single dimensional array name is the starting address of array’s first byte.

–Lower limit of subscript is ZERO whereas Upper limit is n-1. n is size of array

Subscript Operator * () is known as Subscript Operator It has 2 requirements i.e. 1.On left hand side of Subscript Operator there should be a pointer (pointer constant or variable) 2.In between the brackets of Subscript Operator there should be an integer or integral expression whose result is an integer.

Pointer Variable Pointer Constant Due to this statement address of first element will be assigned to ptr.

Behavior of Subscript Operator Using Subscript Operator we can access the elements of array We can do this using pointer variable or pointer constant.

int *ptr, a[5]; 1. ptr = a; // possible 2. a= ptr; // not possible Reason: As per assignment rule on the left side of assignment operator (=) there should be a memory location whose data type should match with RHS value i.e. variable but in line 2 a is a pointer constant not variable so it will give error.

a[2]; OR *(a+2); // 1 write operation ptr[2]; OR *(ptr+2) ; // 1 write operation // 1 read operation

a[2]; *(a+2); Both a and 2 are constants Compiler will directly go to digital address of a i.e. 452 and will add 2 according to the pointer arithmetic Compiler will skip 8 bytes And finally indirection will be performed (i.e.1 write operation)

ptr[2]; *(ptr+2); Ptr is pointer variable and 2 is constant Compiler will access the ptr and will read the address stored in ptr (i.e. 1 read operation) Then it will add 2 according to the pointer arithmetic, and 8 bytes will be skipped. And finally indirection will be performed (i.e.1 write operation)

Multidimensional Arrays in C Multidimensional arrays are simply arrays of arrays (of arrays of arrays...) A multidimensional array like m[3][2] is stored in consecutive memory locations as m[0][0], m[0][1], m[1][0], m[1][1], m[2][0], m[2][1]

Two Dimensional Arrays a[3][5]; a a[0][0] a[0][1]a[0][0]a[0][2] a[1][0] a[2][0] a[0][3]a[0][4]

Two Dimensional Array a[1][4] OR *(a+1)[4] OR *(*(a+1)+4)

Two Dimensional Array Name of two dimensional array is the address of its first row. int a[3][5]; a + 1; // skips 1 row(5 elements)

A complete row will be skipped

Two Dimensional Array Name of two dimensional array is the address of its first row. But after indirection Name of two dimensional array is the address of one element.

Int a [3] [5]; *(*(a+1)+4); a a + 1

Ptr is pointing to a row of 5 integers

int a[2][3][5]; No. of elements= 2*3*5=30 Page 1 Page 2 a

a[1][2][4] Page 1

Two Dimensional Array int a[3][5]; Data type of a is int (*)[5] OR int [5] * int (* ptr)[5]; ptr=a;

Three Dimensional Arrays int a[2][3][5]; Name of three dimensional array is the address of first page in that array.

Three Dimensional Array int a[2][3][5]; Data type of a is int (*)[3][5]; int (* ptr)[3][5]; ptr=a;

Three Dimensional Array a[1][2][3] is equivalent to *(*(*(a+1)+2)+3)

Function Pointer ( ) Function call operator

Operator Precedence C contains many operators, and because of operator precedence, the interactions between multiple operators can become confusing. Operator precedence describes the order in which C evaluates expressions

Operator Precedence ( ) operator has higher precedence then [ ] operator

Function Pointer Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running programme gets a certain space in the main-memory. Both, the executable compiled programme code and the used variables, are put inside this memory. Thus a function name in the program code is nothing else than an address.

Function Pointer int *f1(void) Function returning int * int (*f1)(void) Pointer to function returning int.

Function Pointer int **array[ ]; array of pointers to pointers to int. int *( * array)( ); pointer to function returning int *

Questions double b[2][3][4]; ???????? ptr; // data type of ptr ptr=*b; ++ptr; // how many bytes skipped Read the declarations: int ( *systemptr)(int, long *); Char *(*(*a[50]) (void)) (void);