Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

Similar documents
Pointers, Arrays, and Strings. CS449 Spring 2016

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

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

Intermediate Programming, Spring 2017*

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

CS24 Week 2 Lecture 1

Create a Program in C (Last Class)

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

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

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

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

CSC 211 Intermediate Programming. Arrays & Pointers

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS61C : Machine Structures

ESc101: Pointers and Arrays

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

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

CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

CSC 1600 Memory Layout for Unix Processes"

Arrays and Pointers in C. Alan L. Cox

Principles of C and Memory Management

Arrays and Strings. CS449 Fall 2017

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

o Code, executable, and process o Main memory vs. virtual memory

Arrays and Pointers (part 1)

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

ECE 15B COMPUTER ORGANIZATION

Memory, Arrays & Pointers

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS61C : Machine Structures

Computer Programming: Skills & Concepts (CP) Strings

Chapter 6 - Pointers

Lecture 05 POINTERS 1

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Dynamic Allocation in C

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

Computer Science & Engineering 150A Problem Solving Using Computers

Principles of C and Memory Management

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

Understanding Pointers

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Chapter 7 C Pointers

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

Dynamic Allocation of Memory

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

CS 222: Pointers and Manual Memory Management

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

1d: tests knowing about bitwise fields and union/struct differences.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Course organization. Course introduction ( Week 1)

Structures and Pointers

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

2.2 Pointers. Department of CSE

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Arrays and Pointers (part 1)

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Pointers, Dynamic Data, and Reference Types

Systems Programming and Computer Architecture ( )

CS 61c: Great Ideas in Computer Architecture

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

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

Dynamic Allocation of Memory

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

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

Procedural Programming & Fundamentals of Programming

Arrays, Strings, and Pointers

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Arrays, Strings, and Pointers

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

What have we learned about when we learned about function parameters? 1-1

Dynamic memory allocation (malloc)

What is an algorithm?

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Lecture 05 Pointers ctd..

Memory, Data, & Addressing II CSE 351 Spring

Pointers. Memory. void foo() { }//return

Quick review pointer basics (KR ch )


C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

int Return the number of characters in string s.

K. N. King: chapters 8 and 12

Kurt Schmidt. October 30, 2018

Arrays, Strings, and Pointers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Content. In this chapter, you will learn:

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

C Pointers. 6th April 2017 Giulio Picierro

Introduction to Programming Block Tutorial C/C++

Transcription:

Character Array C Programming Lecture 10-1:A Array &Pointer String A sequence of characters The last character should be \0 that indicates the end of string char greeting[] = "hello"; greeting 0 1 2 3 4 h e l l o 5 \0 same char greeting[] = 'h', 'e', 'l', 'l', 'o', ' 0 ; char myname[8]; gets(myname); String I/O 0 1 2 3 4 5 6 7 myname???????? myname 0 1 2 3 4 5 6 7 L e e \0???? Lee // Character array (string) input/output) int i; char string1[20]; printf("enter the string \"hello there\": ); gets(string1); printf("\nstring1 :\n ); for (i = 0; string1[ i ]!= '\0'; i++ ) printf( %c ); printf( \nthe length of string1 is %d\n,i); If a user gives Lee as a keyboard input following gets() function, myname array will be filled with L L, e, e, 0 0. If keyboard input string is too big (>= 8 characters), an error will occur. $./a.out Enter the string "hello there": hello there string1 : hello there the length of string1 is 11

Pointer Pointer? Usually means memory address Pointer Declaration of Pointer-type variable Declare a variable that stores memory address Pointer operators * (Dereference operator) means the value of & (address-of operator) means address of Example : Pointer Variable and Pointer Operators Memory int *countptr, count; // countptr is pointer // count is an integer type address Variable name int *aptr, *bptr; // aptr and bptr are pointer type aptr = NULL; // aptr is a NULL pointer. NULL = 0 count = 5; // assignment countptr = &count; // countptr points to count that means // the value of countptr is the address of count 5000 5004 5008 countptr countptr count 5 *countptr = 10; // the value countptr points to is changed to 10 // that means count becomes 10 5008 5012 5 count

Pointers and Addresses p int *p = 0; null P=(int*)malloc(sizeof(int)); *p=5 p p 5 Dynamic Allocation ( 동적할당 ) malloc : memory allocation free : memory deallocation int* intptr; char* namestr; intptr nameptr intptr = (int*)malloc(sizeof(int)); // memory allocation namestr = (char*)malloc(sizeof(char)*6);? *p = 55; int q = 44; p = &q; &p p 55 p 55 q 44 &q free(intptr); // deallocation free(namestr); // deallocation Memory Leak : The loss of available memory space that occurs when data is dynamically allocated but never deallocated. Inaccessible object : a dynamic variable on the free store without any pointer pointing to it Dangling pointer : a pointer that points to a variable that has been deallocated. Pointer Arithmetic ti Arrays int *v, *vtemp; v = (int*)malloc(sizeof(int)*5); char charray[20]; charray 0 1 2 18 19............. 3000 3004 3008 3012 3016 3020 charray is an array variable. charray is also a pointer that points to the address of the first array element (= &charray[0]) v v[0] v[1] v[2] v[3]=5 v[4] *charray is the same as charray[0] *(charray+1) is the same as charray[1] vtemp vtemp= v+1; v += 3; *v=5

Almost interchangeable. You must understand! Difference? int b[5]; int *b; int* a=b; int* a=&b[0]; int c = b[1]; int c = *(b+1); Arrays & Pointers char *ptr = string ; ptr s t r i n g \0 Pointer ptr can be used as a form of an array. For example *ptr == ptr[0], *(ptr + 1) == ptr[1],... You must always be careful when using pointers with dynamic allocation especially when using in a function (memory leak). What is Garbage Collection? sizeof operator Returns the size of an array or of any other data type, variable or constant The size of pointer type variable is 4byte in 32bit computer. Example double Array[20]; char a, b[10], *c *d; d = (char*)malloc(sizeof(char)*100); sizeof(array) // 160 sizeof(a) // 1 sizeof(b) // 10 sizeof(c) // 4 sizeof(d) // 4 sizeof(int) // 4 String processing functions Defined in <string.h> char* strcpy(char *s1, const char *s2); char* strncpy(char *s1, const char *s2, size_t n); char* strcat(char *s1, const char *s2); char* strncat(char *s1, const char *s2, size_t n); int strcmp(const char *s1, const char* s2); int strncmp(const char *s1, const char* s2, size_t n); char* strtok(char *s1, const char *s2); size_t strlen(const char *s);

example1 Example2 : swap function void printintvar(char *name, int value) printf("%s\t = %d\n", name, value); int one = 1; int *to_ one; to_one = &one; *to_one one = one + 1; *to_one = *to_one + 1; (*to_one)++; output: one = 1 one = 2 one = 3 one = 4 void swap(int a, int b) int temp; temp=a; a=b; b=temp; int x=3, y=2; printf( before: x=%d, y=%d\n,x,y); swap(x,y); printf( after : x=%d, y=%d\n,x,y);,y); Output : Output : void swap(int* a, int* b) int temp; temp=*a; *a=*b; *b=temp; int x=3, y=2; printf( before: x=%d, y=%d\n,x,y); swap(&x,&y); printf( after : x=%d, y=%d\n,x,y);,y); Example 3 Example 4 char *quote[] = "To err is human, to forgive divine.", "To detect errors is compiler, to correct them is human.", "Time flies like an arrow, fruit flies like a banana." ; int i; const int num = sizeof quote/sizeof *quote; for (i = 0; i < num; ++i) printf("%s\n", quote[i]); #define PRPTR(p) printf("%p\n", p) int nums[] = 1,3,2,4,3,5,4,2; 3 4 2; int *a = nums; int *b = a + 4; printf("sizeof(int) = %d\n", sizeof(int)); PRPTR(a + 0); PRPTR(a + 1); PRPTR(b - 2); PRPTR(b - 1);