LEC20081020 - Introduction to Computer Programming October...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming October 20, 2008 CSC180 Fall 2008, University of Toronto
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Strings A string literal is an array of characters ended with ’ \ 0 ’. char a[] = ‘‘ABCD’’; char a[] = { ’A’, ’B’, ’C’, ’D’, ’ \ 0’ } ; In memory: ’A’ ’B’ ’C’ ’D’ ’\0’ \ 0’ marks the end of a string. CSC180 Fall 2008, University of Toronto 1
Background image of page 2
The compiler treats a string literal as a pointer of type char * . char *a = ‘‘ABCD’’; /* declare and initialize */ Or, char *a; /* declare */ a = ‘‘ABCD’’; /* initialize */ Empty string ‘‘’’ In memory: ’\0’ CSC180 Fall 2008, University of Toronto 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Assignment operator = cannot copy strings. #include <string.h> char src[] = ‘‘source’’; char dest[7]; dest = src; /* WRONG */ strcpy(dest, src); /* correct */ Note: allocate enough space for dest to hold ‘‘source’’ . Don’t forget ‘‘source’’ has a \ 0’
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

LEC20081020 - Introduction to Computer Programming October...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online