43_pdfsam_cs2022 - Arrays and strings: headache summary A...

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

View Full Document Right Arrow Icon
Arrays and strings: headache summary A string is just an array of characters An array is just a pointer to the first element C does not automatically resize arrays for you Once declared, an array has the same size forever C does not keep track of array size for you Use a separate integer to keep track of it Or you can use a special ending symbol, e.g. ‘\0’ for strings C does not do bounds checking
Background image of page 1

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

View Full DocumentRight Arrow Icon
String I/O functions printf() and scanf() have a %s placeholder for string I/O Also available are gets() and puts() #include <stdio.h> int main(){ char string[1024]; scanf(“%s”, string); printf(“You entered: %s\n”, string); gets(string); printf(“You entered: “); puts(string); return 0; }
Background image of page 2
Buffer overruns What if someone enters more than 1024 characters? Remember: C doesn’t check for array out-of- bounds errors scanf copies the input to memory, past the end of the space allocated for the string
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

43_pdfsam_cs2022 - Arrays and strings: headache summary A...

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

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