String Constants - s1 = calloc (20, sizeof (char)); s2 =...

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

View Full Document Right Arrow Icon
9: String Constants char *name = ”Skate"; The block of memory for a string constant may be stored in "read-only" memory, and its contents should not be modified; therefore, do not reset any of the characters in the constant string:
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 and Characters Double and single quotes that enclose a single character signify different things: "W" denotes a pointer to a memory block containing two characters: W \0
Background image of page 2
9: Strings Parameters C strings can be used as parameters as any other pointers void modify(char *s) { s[0] = toupper(s[0]); } char *p; /* modify(p); */ if((p = calloc(10, sizeof(char))) == NULL) error "Memory allocation" Idiom "String suffix" Idiom
Background image of page 3

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

View Full DocumentRight Arrow Icon
9: Strings Parameters /* Same as strlen() */ int length(const char *s) { char *p; for(p = s; *p ; p++) /* *p != '\0' */ ; return p - s;
Background image of page 4
Traversing a String for(p = s; *p; p++) use *p
Background image of page 5

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

View Full DocumentRight Arrow Icon
In-class Exercise (Individual) Consider the following program fragment: char *s1, *s2;
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s1 = calloc (20, sizeof (char)); s2 = malloc (40 * sizeof (char)); strcpy (s2, There is a tide); Assume both memory allocations l strlen(s1) l s2[strlen(s2)] l *(s1 + 7) l strlen (s1 + 9) l strcpy (s1, s2) strdup char *strdup(const char *s) { /* return copy of s */ char *kopy; /* copy of s */ char *ps; /* used for copying */ char *pkopy; /* for copying */ if((kopy =calloc((length(s)+1),sizeof(char)))==NULL) return NULL; /* memory allocated, now copy */ for(ps = s, pkopy = kopy; *ps; ps++, pkopy++) *pkopy = *ps; *pkopy = *ps; "Memory allocation" Idiom "String Traversal" Idiom modify char *modify(const char *s) { /* return a copy of s modified */ char *news; if((news = strdup(s)) == NULL) return NULL; news[0] = toupper(news[0]); return news; "String suffix" Idiom "i-th character" Idiom...
View Full Document

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 8

String Constants - s1 = calloc (20, sizeof (char)); s2 =...

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

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