Precept 3 : C Arrays, Strings, and Pointers

Slides:



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

Lectures 10 & 11.
Computer Programming w/ Eng. Applications
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
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).
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.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
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.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
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.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
The C++ Data Types Fundamental Data Types
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Computer Skills2 for Scientific Colleges
Characters and Strings
Pointers in C.
Pointers and Pointer-Based Strings
Pointer Data Type and Pointer Variables II
INC 161 , CPE 100 Computer Programming
Programming Languages and Paradigms
Computer Science 210 Computer Organization
Chapter 15 Pointers, Dynamic Data, and Reference Types
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Programming with Pointers
Computer Skills2 for Scientific Colleges
Homework Starting K&R Chapter 5 Good tutorial on pointers
C++ Pointers and Strings
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Exercise Arrays.
Chapter 9: Pointers and String
Lecture 19: Working with strings
CS410 – Software Engineering Lecture #3: C++ Basics I
CISC181 Introduction to Computer Science Dr
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Programming fundamentals 2 Chapter 3:Pointer
Presentation transcript:

Precept 3 : C Arrays, Strings, and Pointers 2012.09.19.

Pointer-Related Operators Key p, p1, p2 : pointer variables i : integral expression Operators for any pointer variable Dereference operator *p : contents of the memory referenced by p Equality and inequality relational operators p1 == p2 : 1 if p1 is equal to p2, 0 otherwise p1 != p2 : 1 if p1 is not equal to p2, 0 otherwise Assignment operator p1 = p2 : assign p2 to p1

Pointer-Related Operators Operators for pointers that reference array elements Arithmetic operators p + i : address of ith element after the one referenced by p i + p : address of ith element after the one referenced by p p – i : address of ith element before the one referenced by p p++ : increment p to point to the next element. The new value of p. ++p : increment p to point to the next element. The new value of p. p-- : decrement p to point to the previous element. The new value of p. --p : decrement p to point to the previous element. The new value of p. p1 – p2 : the “span” of p1 and p2.

Pointer-Related Operators Relational operators p1 < p2 : 1 if p1 is less than p2, 0 otherwise. p1 <= p2 : 1 if p1 is less than or equal to p2, 0 otherwise. p1 > p2 : 1 if p2 is greater than p2, 0 otherwise. p1 >= p2 : 1 if p2 is greater than or equal to p2, 0 otherwise. Assignment operators p += i : increment p so its value is the address of the ith element after the one referenced by p. The new value of p. p -= i: decrement p so its value is the address of the ith element before the one referenced by p. The new value of p.

Pointer-Related Operators Disallowed p1 + p2 i – p i += p i -= p p == I Array subscripting operator p[i] : *(p + i), the contents of memory at the address that is i elements after the address referenced by p.

Kinds of Function Parameters Kind of parameters Example Implementation C Construct in max() (both params) Call by value Ordinary parameter out scanf() (2nd param) Call by reference Pointer parameter inout swap()

The “const” Keyword with Pointers Pointer to constant Constant pointer 1: const int i1 = 100; 2: const int i2 = 200; 3: const int *pi = &i1; /* pi is a "pointer to a constant." */ 4: i1 = 300; /* Error. Cannot change i1. */ 5: i2 = 400; /* Error. Cannot change i2. */ 6: pi = &i2; /* OK. */ 7: *pi = 500; /* Error. Cannot change *pi. */ 1: int i1 = 100; 2: int i2 = 200; 3: int *const pi = &i1; /* pi is a "constant pointer." */ 4: i1 = 300; /* OK. */ 5: i2 = 400; /* OK. */ 6: pi = &i2; /* Error. Cannot change pi. */ 7: *pi = 500; /* OK. */

The “const” Keyword with Pointers Constant pointer to constant 1: const int i1 = 100; 2: const int i2 = 200; 3: const int *const pi = &i1; /* pi is a "constant pointer to a constant." */ 4: i1 = 300; /* Error. Cannot change i1. */ 5: i2 = 400; /* Error. Cannot change i2. */ 6: pi = &i2; /* Error. Cannot change pi. */ 7: *pi = 500; /* Error. Cannot change *pi. */

The “const” Keyword with Pointers Disallowed mismatch Disallowed mismatch in function calls 1: const int i1 = 100; 2: const int i2 = 200; 3: int *pi = &i1; /* Error. Subversive. Subsequently changing *pi would change i1. */ 1: void f(int *pi) 2: { 3: ... 4: } ... 5: const int i1 = 5; 6: const int *pi2 = &i1; 7: f(pi2); /* Error. Subversive. If f() changes *pi, then *pi2 also would change. */

The “const” Keyword with Pointers Allowed mismatch Allowed mismatch in function calls 1: int i1 = 100; 2: int i2 = 200; 3: const int *pi = &i1; /* OK, even though subsequently changing i1 would change *pi. */ 4: i1 = 300; /* OK. Also changes *pi. */ 5: i2 = 400; /* OK. */ 6: pi = &i2; /* OK, even though subsequently changing i2 would change *pi. */ 7: *pi = 500; /* Error. Cannot change *pi. */ 1: void f(const int *pi) 2: { 3: ... 4: } ... 5: int i1 = 5; 6: int *pi2 = &i1; 7: f(pi2); /* OK. *pi2 is protected against accidental change by f(). */

Manipulating C Strings String operation String in stack String in rodata section Allocating memory for a string { char acStr[5]; … } Initializing a string char acStr1[3] = {'h', 'i', '\0'}; char acStr2[] = {'h', 'i', '\0'}; char acStr3[3] = "hi"; char acStr4[] = "hi"; char acStr5[2] = "hi"; /* truncation */ char acStr6[10] = "hi"; ... …“hi”…

Manipulating C Strings Computing the length of a string { char acStr[10] = "hello"; ... strlen(acStr) /* Evaluates to 5 */ sizeof(acStr) /* Evaluates to 10 */ } char *pcStr = "hello"; strlen(pcStr) /* Evaluates to 5 */ sizeof(pcStr) /* Evaluates to 4 */ Changing the characters of a string char acStr[10] = "hi"; acStr = "bye"; /* compiletime error */ acStr[0] = 'b'; acStr[1] = 'y'; acStr[2] = 'e'; acStr[3] = '\0'; strcpy(acStr, “hello world"); /* Danger of memory corruption. */ (Runtime error to attempt to change the characters of a string that resides in the rodata section)

Manipulating C Strings Concatenating characters onto a string { char acStr[10] = "hi"; acStr += "bye"; /* compiletime error */ acStr[2] = 'b'; acStr[3] = 'y'; acStr[4] = 'e'; acStr[5] = '\0'; strcat(acStr, “hello world"); /* Danger of memory corruption. */ } (Runtime error to attempt to change the characters of a string that resides in the rodata section) Comparing one string with another char acStr1[] = "hi"; char acStr2[] = "bye"; if (acStr1 < acStr2) ... /* Legal, but compares addresses!!! */ if (strcmp(acStr1, acStr2) < 0) ... /* Compares strings */ (Same as string in stack)

Manipulating C Strings Reading a string { char acStr[10]; iConvCount = scanf("%s", acStr); /* Reads a word as a string. Grave danger of memory corruption. */ iRet = gets(acStr); /* Reads a line as a string, removing the \n character. Grave danger of memory corruption. */ iRet = fgets(acStr, 10, stdin); /* Reads a line as a string, retaining the \n character. */ } (Runtime error to attempt to change the characters of a string that resides in the rodata section)

Manipulating C Strings Writing a string { char acStr[] = "hi"; iCharCount = printf("%s", acStr); /* Writes a string. */ iSuccessful = puts(acStr); /* Writes a string, appending a \n character. */ iSuccessful = fputs(acStr, stdout); } (Same as string in stack)

Manipulating C Strings Converting a string to another type { char acStr[] = "123"; int i; long l; double d; iConvCount = sscanf(acStr, "%d", &i); i = atoi(acStr); l = atol(acStr); d = atof(acStr); } (Same as string in stack) Converting another type to a string char acStr[10]; int i = 123; iCharCount = sprintf(acStr, "%d", i); /* Danger of memory corruption. */ (Runtime error to attempt to change the characters of a string that resides in the rodata section)