L09_Strings - CS1372 CS1372: HELPING TO PUT THE COMPUTING...

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

View Full Document Right Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Strings
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings Important: There are no strings in C
Background image of page 2
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings Many languages include built-in support for Strings Typical examples include Basic Pascal Scheme/Lisp Java "Built-in" here implies behind the scenes details are managed by the compiler and/or the run-time environment
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings in C There are no Strings in C. There is an agreed upon convention to store string like data structures in arrays of characters. Part of the convention includes terminating strings with a null character. In many cases "string" operations in C look just like those found in other languages. Do not be deceived. This is C. ..memory must be managed properly (by you)!
Background image of page 4
CS1372: HELPING TO PUT THE COMPUTING IN ECE Compare Basic 10 a$ = "Hello " 20 b$ = "world" 30 a$ = a$ + b$ 40 print a$ run Hello World C char *a = "Hello "; char *b = "world"; strcat(a, b); printf("%s\n", a);
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE Better? char a[12] = "Hello "; char *b = "world"; strcat(a, b); printf("%s\n", a);
Background image of page 6
CS1372: HELPING TO PUT THE COMPUTING IN ECE What's happening behind the scenes in Basic? Who cares!?!?!?
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE What's happening in C? char a[12] = "Hello "; H e l l o \0 ? a
Background image of page 8
CS1372: HELPING TO PUT THE COMPUTING IN ECE What's happening in C? char a[12] = "Hello "; char *b = "world"; H e l l o \0 a w o r l d \0 b
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE What's happening in C? char a[12] = "Hello "; char *b = "world"; strcat(a, b); H e l l o a w o r l d \0 b w o r l d \0
Background image of page 10
CS1372: HELPING TO PUT THE COMPUTING IN ECE What's happening in C? char a[12] = "Hello "; char *b = "world"; strcat(a, b); printf("%s\n", a); H e l l o a w o r l d \0 b w o r l d \0 Hello world
Background image of page 11

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings We implement string-like data structures using character arrays char s1[10]; /* Not a string! */ char *s2; /* Not a string! */ char s3[10] = "foo"; /* Not a string */ char *s4 = "bar"; What acts like a string? A pointer to a character followed by a sequence of characters terminating in a null byte null byte char s5[10] = "foo"; ‘f’ ‘o’ ‘o’ ‘\0’ ‘??’ ‘??’ ‘??’ ‘??’ ‘??’ ‘??’ undefined
Background image of page 12
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings char s1[10] = "foo"; printf("%s\n", s1); ham_VBAText("%s\n", s1); sprintf(buffer, "%s\n", s1); All work work to print foo Note: If s1 is not null terminated the printf will print a character string until it encounters a null character or causes an error (segment fault etc.)
Background image of page 13

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

View Full DocumentRight Arrow Icon
CS1372: HELPING TO PUT THE COMPUTING IN ECE Strings char s1[10]; s1[0] = 'f'; s1[1] = 'o'; s1[2] = 'o'; printf("%s\n", s1); Sample output fooâ-•VGot 2 ,_VGot 2 Got 20 _VGot 2 _Got 16 Got 1
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/07/2012 for the course CS 1372 taught by Professor Smith during the Spring '07 term at Georgia Institute of Technology.

Page1 / 55

L09_Strings - CS1372 CS1372: HELPING TO PUT THE COMPUTING...

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

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