Character Strings. String-copy Example

Similar documents
Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CS 61c: Great Ideas in Computer Architecture

ECE 15B COMPUTER ORGANIZATION

Arrays and Pointers (part 1)

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C : Machine Structures

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Data Representation and Storage. Some definitions (in C)

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Arrays and Pointers (part 1)

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Fundamental of Programming (C)

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Data and File Structures Laboratory

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

In Java we have the keyword null, which is the value of an uninitialized reference type

CS61C : Machine Structures

CSC 1600 Memory Layout for Unix Processes"

Course organization. Course introduction ( Week 1)

Dynamic memory. EECS 211 Winter 2019

Understanding Pointers

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Intermediate Programming, Spring 2017*

Data Representation and Storage

More about BOOLEAN issues

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

ESC101N: Fundamentals of Computing End-sem st semester

Memory Management. CSC215 Lecture

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

EM108 Software Development for Engineers

PDS Class Test 2. Room Sections No of students

Heap Arrays. Steven R. Bagley

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

POINTER AND ARRAY SUNU WIBIRAMA

Array Initialization

Language comparison. C has pointers. Java has references. C++ has pointers and references

CS 11 C track: lecture 5

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Memory Allocation. General Questions

M.CS201 Programming language

Advanced Pointer Topics

Low-Level C Programming. Memory map Pointers Arrays Structures

First of all, it is a variable, just like other variables you studied

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Heap Arrays and Linked Lists. Steven R. Bagley

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Multidimension array, array of strings

... Lecture 12. Pointers

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Pointers, Dynamic Data, and Reference Types

CSCI 171 Chapter Outlines

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Kurt Schmidt. October 30, 2018

Content. In this chapter, you will learn:

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

C++ for Java Programmers

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

POINTER & REFERENCE VARIABLES

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

C PROGRAMMING Lecture 4. 1st semester

Dynamic Memory Allocation and Linked Lists

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Contents of Lecture 3

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

CS 241 Data Organization Pointers and Arrays

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

ECE264 Fall 2013 Exam 3, November 20, 2013

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Principles of C and Memory Management

Procedural Programming & Fundamentals of Programming

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Dynamic Allocation of Memory Space

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Transcription:

Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789"; defines s to be an array of 11 chars Functions are passed pointers to the beginning of a string String-copy Example Several implementation of string-copy Copies character string pointed to by t to the area pointed to by s void strcpy(char *s, char *t); int i = 0; while ( (s[i] = t[i])!= \0 ) /* array version */ i++; while ( (*s = *t)!= \0 ) /* pointer version */ {s++; t++;} while ( (*s++ = *t++)!= \0 ) /* short pointer version */ while ( *s++ = *t++) /* shorter pointer version */ 1

Arrays vs. Pointers The difference between: char a[] = "012"; /* array of char */ char *b = "012"; /* pointer to constant array */ b[1] = '5'; /* undefined */ b = a; /* makes b point to a */ String Arguments Equivalent forms of function arguments int strlen( char s[]); int strlen(char *s); Any part of the array can be passed as function argument: char a[] = "0123456789"; strlen(&a[3]); /* pass part of array */ 2

Array of Pointers char *x[10]; /* x is an array of 10 pointers to char */ char *color[] = { red, green, yellow, blue }; Defines color to be an array of pointers to char, each pointing to a constant character string. Color r e d \0 g r e e n \0 y e l l o w \0 b l u e \0 Multi-dimensional Arrays Int magic[3][3] = { {6, 7, 2}, {1, 5, 9}, {8, 3, 4}}; A two dimensional array is an array of arrays magic: [0] [1] [2] magic[0] This explains the notation magic[1] magic[i][j] rather than magic[2] magic[i,j] 3

Passing Multi-dim Arrays to Funs void print(int a[][]) { int i, j; for (i = 0; i < MAXROWS; i++) { for (j = 0; j < MAXCOL; j++) printf("%d ", a[i][j]); printf("\n"); } } Will lead to compiler error! Why? Why is void print(int a[]) fine? Parameter declaration must include number of cols Structures A way to pack different data types as a unit Useful to keep track of mixed data: payroll records student records geometrical shapes data structure nodes 4

Basics of Structures struct tag { member declarations } declared variables; A structure declaration defines a type struct tag { }; /* no storage is allocated */ struct { } x, y, z; int x, y, z; struct tag x, y, z; Examples and Initialization struct point { }; int x; int y; struct point p = { 2, 3}; printf("%d %d\n", p.x, p.y); /* accesses members */ struct triangle { /* a new type: triangle */ }; struct point p1; struct point p2; struct point p3; 5

Structures and Pointers Structures are passed to functions by value This is not efficient for large structures imagine copying thousands of fields How can this be improved? Pass a pointer to a structure still by value but only 4 bytes struct point *pp; declares pp to point an object of type struct point how much storage is allocated? Accessing Members struct point origin; struct point *pp = &origin; Recall that *pp can be used wherever origin is used (*pp).x and (*pp).y Are the brackets needed? Alternative notation pp->x, pp->y are equivalent to (*pp).x, (*pp).y 6

Self-referential Structures Contains a pointer to an object of its own type struct node { int key; struct node *next; }; Why a recursive structure should not be allowed? Typedef typedef: creates new data type from the old typedef int Length; /* Length is now a synonym for int */ typedef struct point { int x; int y; } PointType; PointType p1, p2; 7

Dynamic Memory Allocation Helps create and maintain dynamic data create storage when demanded release when no longer needed Functions malloc, free and the operator sizeof play a key role void *malloc(size_t size); allocates at least size bytes and returns a pointer to the beginning of the memory block returns NULL if something goes wrong free and sizeof void free(void *ptr); releases the block pointed to by ptr the block should have been allocated by malloc or other functions in the family sizeof operator: determines in bytes the size of its operands char a[3]; typedef struct point { int x; char y;} PointType; printf("sizeof char = %d, a = %d, PointType = %d \n", sizeof(char), sizeof a, sizeof(pointtype)); sizeof char = 1, a = 3, PointType = 8 8

Pointer Arithmetic If p points to an object, then p + i points to the i-th object of the same type (i an int) char c[10], *cp; int i[10], *ip; float b[2][20], (*bp)[20]; /* a points to an arrray */ cp = c; ip = i; bp = &b[0]; printf("cp = %p, cp+1 = %p, ip = %p, ip+2 = %p, bp = %p, bp+1 = %p\n", cp, cp+1, ip, ip+2, bp, bp+1); bp++; printf(" bp - &b[0] = %d\n", bp - &b[0]); /* produces and int */ cp = ffbefa6a, cp+1 = ffbefa6b, ip = ffbefa3c, ip+2 = ffbefa44, bp = ffbef998, bp+1 = ffbef9e8 bp - &b[0] = 1 9