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.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

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.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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.
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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
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.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Pointers CSE 2451 Rong Shi.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
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.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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]
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
CSC Programming for Science Lecture 34: Dynamic Pointers.
Computer Programming for Engineers
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 8 Arrays, Strings and Pointers
Pointers.
Command-Line Arguments
Hassan Khosravi / Geoffrey Tien
14th September IIT Kanpur
Programming with Pointers
Outline Defining and using Pointers Operations on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Exercise Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

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 cannot be changed by an assignment statement or by any other means. A pointer is a variable whose address value can be changed by assignment.

Using Array Name float on_time_rate[100]; float *ptr; ptr = on_time_rate; Or equivalently ptr = &on_time_rate[0]; Once ptr is assigned value, you can use ptr as an array, e.g., ptr[0], ptr[1], …, ptr[99] This is the same as *ptr, *(ptr+1), …, *(ptr+99)

Pointer to char and Array of char char *s = "My Brother"; Declares a pointer s to char, initializes it to the starting address of a string. char c[]= "My Car"; Declares an array c of size 7, with c[0]='M', c[1]='y', …, c[5]='r', c[6] ='\0'. Then we can say s = c; Or equivalently s = &c[0]; But NOT c = "My hand"; or c = s;

Use pointer without Initialization is an error char *s; scanf("%s", s); Error: pointer has no value! Correct one should be: char *s; char str[10]; s = str; scanf("%s", s); or scanf("%s", str); or scanf("%s", &str[0]); But scanf("%s", &s); is wrong.

Equivalence between Array Indexing and Pointer Dereferencing If p is either and array name or pointer, then the following two forms are equivalent: p[k]  *(p+k) In particular p[0]  *p Thus, we can use pointer as if it were an array, or array as if it were pointer. The only difference is that array name is a constant, while pointer is a variable.

Pointer Operation The following pointer operations are allowed: –Adding an integer to a pointer, e.g., p + 2. –Subtracting an integer from a pointer, e.g., p - 1. –Subtracting two pointers, e.g., p1 - p2. –Comparing pointers using relational operator, e.g., p1 >= p2. p+1 means the address of the next cell of type which p is pointing to, I.e., if *p is p[0], then *(p+1) is p[1].

Pointer Arithmetic #include main() { char alpha[]={'A', 'B', 'C', 'D', 'E'}; char x, *p1, *p2; p1 = alpha; p2 = p1 + 2; x = *p2; printf("%c%c%c", x, *p1, *(p2-1)); } The program prints: CAB

Compute the String Length Again #include main() { printf("%d\n", length("This")); } int length(char *s) { char *p = s; while(*s) ++s; return (s-p); }

Call by Value and Call by Reference Call by value: The function works with a copy of the argument sent to it. Call by reference: The function receives reference to variable and works directly with the original variable. C programming language does not support call by reference. But its effect can be simulated with pointer.

Simulate Call by Reference #include void f(int x, int *p); main() { int x = 998; int y = 998; f(x,&y); printf("%d %d\n", x, y); } void f(int x, int *p) { x += 2; *p += 2; } The program prints:

Reversing a String in Place #include void rev(char *s); main() { char str[101]; scanf("%s", str); rev(str); printf("%s", str); } void rev(char *s) { char t, *e; e = s + strlen(s) -1; while(s < e) { t = *s; *s++ = *e; *e-- = t; }

Pointers and Multidimensional Array The equivalence *(p+k)  p[k] applies also for multidimensional array. Thus we have **q  *q[0]  q[0][0] q[i][j]  *(q[i]+j)  *(*(q+i)+j) In particular, if we declare int q[5][5]; Then q is a pointer to pointer to int value.

Mixing Array Indexing and Pointer Dereferencing #include main() { int q[3][3] = {{0,1,2},{3,4,5}, {6,7,8}}; printf("%d %d %d %d\n", q[1][2], *(q[1]+2), *( (*(q+1) + 2) ), *(&q[0][0] + 5) ); } Prints: 5 5

Fixed Size Two- Dimensional Array #include void print_names(char p[][11], int n); main() { char phys[4][11] = {"Newton", "Einstein", "Fermi", "Heisenberg"}; print_names(phys, 4); } void print_names(char p[][11], int n) { int i; for(i = 0; i < n; i++) printf("%s\n", *p++); }

Array of Pointers #include void print_names(char *p[], int n); main() { char *phys[4] = {"Newton", "Einstein", "Fermi", "Heisenberg"}; print_names(phys, 4); } void print_names(char *p[], int n) { int i; for(i = 0; i < n; i++) printf("%s\n", *p++); }

Reading/Home Working Read Chapter 7, page 321 to 379. Work on Problems –Section 7.3, page 337, exercise 1, 3, 5, 7. –Section 7.4, page 349, exercise 1, 3, 5. –Section 7.6, page 367, exercise 3, 5. Check your answers in the back of the textbook. Do not hand in.