Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

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.
Programming and Data Structure
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Pointers Applications
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
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.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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:
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Pointers *, &, array similarities, functions, sizeof.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
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.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2016 Pearson Education, Ltd. All rights reserved.
INC 161 , CPE 100 Computer Programming
Programmazione I a.a. 2017/2018.
Outline Defining and using Pointers Operations on pointers
Variables and Constants
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Computer And Programming Array and Pointer

Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to accessing memory that has been allocated by other means, often as an array 2

One Dimensional Array A (one-dimensional) array is a series of objects, all of which have the same type. An array is a block of memory made up of memory locations of a fixed size. –declares a as an array of five int objects –declares x as an array of 100 long double objects int a[5]; 3 long double x[100];

Array Initialization We can initialize an array when it is declared by listing the initial values for its elements, as follows: Array with five elements has subscript ranging from 0-4. The values assigned to the array a can be illustrated in figure below: int a[5] = {75, 25, 100, -45, 60}; a[0] a[1] a[2] a[3] a[4]

Accessing Array Subscripted array can be used like variable names, such as x and y. For example: –assigns 5 to element 2 (the third element) of a, –sets n to the difference of elements i and j, and –increments the value of element 2*i-j. –(see postfix and prefix operations) a[2] = 5; 5 n = a[i] – a[j]; a[2*i – j]++;or ++a[2*i – j];

Declaring Array with Constant Constants represented by macros are frequently used in declaring and manipulating arrays. For example, if COUNT is defined by –then –declares c as an array of 1000 int objects. #defineCOUNT int c[COUNT];

Array and Loops An example of setting all elements to 0 is given below An equivalent implementation is also given as: for(i = 0; i < COUNT; i++) c[i] = 0; 7 int i = 0; while(i < COUNT) c[i++] = 0;

Pointers A pointer is the address of an object of a particular type. An address only locates the first byte of an object. We need the type because it can determine the size of the object and the meaning of the bits stored in it. Typical pointer types are pointer-to-int, pointer-to-long, pointer-to-double, and so on. 8

Indirection Pointer If p is a variable with type pointer-to-int, the value of p is the address of an int object. Thus, *p is an lvalue of an object that is pointed by p p *p 75 p P is a pointer to an object. The value of p is the address of the object. Thus, the value of *p is the value of the object pointed by pointer p.

int *p; // Declare p as a pointer-to-int type *p = 20;// assigns 20 to the *p. And printf(“%d”, *p);// prints the value 20, and n = *p + 10;// assigns 35 to n, *p += 7;, which is equivalent to *p = *p +7; // adds 7 to the *p, then stores the result to *p, whereas (*p)++;// increments the value of the target object. Example of Using a Pointer 10

We can use the address-of operator to compute the address of any object designated by an lvalue If we declare –then &n has type pointer-to-int and &x has type pointer-to-double. If we now declare –then p is initialized to point to n. Thus, *p and n name the same object –They are said to be aliases Using the Address-of Operator int n = 100; double x = 3.5; 11 int *p = &n;

Using const in Pointer Declaration In pointer, const can be used in two difference places. If const is placed at the beginning of the declaration, the target object is declared constant. In this case, value of target object *p cannot be changed. Thus the following statements are invalid. if m and n are integer, then following statements can be applied to p. const int *p; 12 *p = 100;and(*p)--;

Using const in Pointer Declaration (cont) If const precedes the pointer variable, then the value of p (address of which p points to) now becomes constant The following statements are not allowed. However, these statements can be applied to p. 13 int *const p = &m; p = &n; andp++; *p = 25; *p += 3; (*p)++; (*p)--;

Pointers and Arrays We can use the name of an array to initialize a pointer to the address of the first element of the array. Both p and q point to the first element of list. C allows the subscript operator to be applied to pointer as well as array names. –It is legitimate to use p[0] to refer to the first element of list. –It is also possible to refer to the 5 th element of list by using p[4] int list[100]; int *p = list; int *q; q = list; 14

Pointers and Arrays (cont) So, we can use It can also be written as sum = 0; for(i = 0; i < 100; i++) sum += list[i]; 15 sum = 0; for(i = 0; i < 100; i++) sum += p[i]; A pointer need not always point to the first element of an array. We can set p to point to element 50 of list : p = &list[50];

Pointers and Arrays Example A pointer need not always point to the first element of an array. We can set p to point to element 50 of list : p = &list[50]; 16 p list[48], p[-2] list[49], p[-1] list[50], p[0] list[51], p[1] list[52], p[2] list (Keep in mind, p must point to a valid location of the target object.)

String Strings are stored in memory as arrays of char values. The null character ('\0') is used to indicate the end of string We can check it by trying this statement –Computer will print printf(“abcd\0efgh”); 17 abcd

String Declaration We can declare an array of characters to hold a string –This will make a string containing up to 3 characters –The number of charatered used will be one less than the declaration because it needs tell the termination of string (null) String initilization could be as follow: –It can also be written as char s[4]; 18 char s[4] = “dog”; char s[4] = { ‘d’, ‘o’, ‘g’, ‘\0’ };

String Declaration (cont) We can also leave it to compiler to determine its size char s[] = “dog”; 19 s[0] s[1 ] s[3] 'd' 'o' 'g' '\0' s[2 ] s

String Declaration (cont) If we declaration statement as –it will result in We can print it using 20 char s[10] = “dog”; s[0]s[1]s[2]s[3]s[4]s[5]s[6]s[7]s[8]s[9] 'd''o''g''\0' Unused printf(s); or printfs(“%s”, s);

String Pointer We can assign pointer-to-char to array of character(string). The initialization can be as follow: We can then print it likewise: char *p = s; 21 char *p = “dog”; printf(p); or printf(“%s”, p);