COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Introduction to C Programming
C Programming lecture 2 Beautiful programs Greek algorithms
Programming in C Chapter 10 Structures and Unions
An introduction to pointers in c
Course Introduction Bryce Boe 2013/06/25 CS24, Summer 2013 C.
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.
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.
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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
C Programming - Lecture 5
Week 7 - Monday.  What did we talk about last time?  Pointers to pointers  Lab 6.
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Exercise 10 Review: pointers, strings and recursion.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
1 CSC241: Object Oriented Programming Lecture No 22.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
1 CS161 Introduction to Computer Science Topic #16.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Today’s Material Strings Definition Representation Initialization
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.
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.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
13. 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.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Motivation and Overview
Lecture 8 String 1. Concept of strings String and pointers
CSE 303 Lecture 14 Strings in C
C Stuff CS 2308.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Chapter 8 Character Arrays and Strings
Arrays as pointers and other stuff
Presentation transcript:

COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez

Administrative stuff Did you guys have a good break? Quiz #5 was due 2 minutes ago! (give or take) Friday (7/3) is the observed day for Independence Day – No class No quiz this week

Administrative stuff Homework #4 is due Thursday. How is that going? Who has finished the interactive portion? (Receiving plays) Who has finished coding the losing condition? Who has finished the open procedure? Anyone has worked on the extra credit?

Quick overview of last class Character strings store text! char * ; //pointer char [ ]; //array char *str = "this works fine"; printf("%s", str); Console arguments int main (int argc, char **argv); //more standard int main (int argc, char *argv[]); //easier to understand

Length of a string int length(char *s) { int len = 0; while(s[len] != '\0') { len++; } return len; }

Length of a string int length(char *s) { int len = 0; while(s[len]) { len++; } return len; } Slightly more efficient!

User string input Let’s say we have a piece of code like this one: char *str; scanf("%s", str); //notice that this one doesn’t need & printf("%s", str); What do you think will happen? Let’s test it

User string input Ok, so we need to allocate the string. char str[100]; //using array notation makes sense here. scanf("%s", str); //notice that this one doesn’t need & printf("%s", str); What happens when we try to read more than one string?

User string input char s1[100]; //using array notation makes sense here. char s2[100]; //using array notation makes sense here. scanf("%s%s", s1, s2); //still we don’t use & for strings printf("%s\n", s1); printf("%s\n", s2);

Comparing strings In the previous one. How do I know if the two strings are the same? For example, the user inputs: Independence Could I compare if(s1 == s2) Why or why not?

Comparing pointer types In general, == compares values. “Values” when it comes to pointers refer to positions in memory So when I do s1 == s2, the code I’m generating is: Are s1 and s2 pointing to the same location in memory? NOT: Are s1 and s2 storing the same value. This is the same for arrays and pointers (wait for it…)

Comparing pointer types So if I do: int arr1[2] = {0,1}; int arr2[2] = {0,1}; The same problem arises.

Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i] ) { i++; } return s1[i] == '\0' && s2[i] == '\0'; }

Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] &&s2[i] && s1[i] == s2[i] ) { i++; } return s1[i] == '\0' && s2[i] == '\0'; }

Comparing strings int equal(char *s1, char *s2) { int i = 0; while(s1[i] &&s2[i] && s1[i] == s2[i] ) { i++; } return !(s1[i] || s2[i]); } Isn’t this one beautiful???? OK, OK, maybe it’s just me

Comparing strings While understanding those functions is important to our understanding of strings. Truth is we rarely have to write that particular function (or the length one for that matter). defines a lot of useful functions. strcmp – compares two strings (and does more that our equal function) strcmp strlen – finds the length of a string. strlen

Comparing structs? This produces a similar problem. However, this is not allowed in compilation. == as an operator, just doesn’t know how to compare non-standard types. To compare structs, you need to define functions.

Pointers

Ok… so what’s a pointer? Let’s first talk about “indirection”