Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]

Slides:



Advertisements
Similar presentations
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Advertisements

Lectures 10 & 11.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Pointers in C Rohit Khokher
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Introduction to C programming
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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 in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers and Dynamic Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
CSCI206 - Computer Organization & Programming
Object Oriented Programming COP3330 / CGS5409
Pointers and Pointer-Based Strings
Lecture 2 Arrays & Pointers September 7, 2004
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Charles Clute Tom Most Michael Hein

Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6] = "STRG01"; char volume[] = "STRG01";

Creating New Strings  Creating garbage strings  Creating strings with given characters  Creating strings with given characters and size  Creating a pointer to a string (dynamic allocation) char str[6]; char str[6] = "STRG01"; char str[] = "STRG01"; char* str = string_name; // no & necessary

 Characters in strings can be created in two ways: integral values and character literals (surrounded by “'”)  Strings in C end at the first null byte (use strlen() to get the length)  Null character: str[0] = 'S'; str[0] = 0xE2; // hex literals are handy char zerobyte = '\0'; // escape character char zerobyte = 0; // or 0x00 hex

String variables have an implied pointer which has special properties:  It has the string name (no subscripts)  It is a constant (you can't alter its value)  The sizeof operator returns the size of the array, not that of a pointer.

 Strings can be looped though in two different ways: using subscripts and using pointers. Subscript: int i; char str[6]; for (i = 0; i < sizeof(volume); i++) str[i] = '0'; Pointer: char str[6]; char *ptr; for (i = 0, ptr = str; i < sizeof(volume); ptr++, i++) *ptr = i;

strtok()  Syntax: #include char* strtok(char *str1, const char *str2);  The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token.

strstr()  Syntax: #include char *strstr(const char *haystack, const char *needle);  The function strstr() returns a pointer to the first occurrence of needle in haystack, or NULL if no match is found.

Other String Functions  strcpy() — copies one string to another (buffer overflow, ahoy!)  strcat() — appends one string to another  strchr() — finds a character in a string (there are several others in this vein)  strlen() — finds the length of the string (slow!)  memcpy() — copies blocks of memory. Fast and safe.  memmove() — like memcpy(), but works if the blocks overlap

Pointers  Function Pointers When dereferenced, a function pointer invokes a function, passing it zero or more arguments just like a normal function. Syntax: void (*foo)(int a); foo = my_int_func; // (no &)  Void Pointers A pointer to void is a generic pointer that can be used to hold an address, but cannot be 'deferenced': that is to say, you can't use it with an asterisk before it to update a variable.