lecture07 - Today: * * * * * * Finish arrays and pointers...

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

View Full Document Right Arrow Icon
Today: * Finish arrays and pointers * The const qualifier * Arguments to programs: argc and argv * Stream I/O * Buffering * File I/O Here's another example: void strcpy(char *dest, const char *src) // REQ: src is a NULL-terminated string dest is big enough to hold a copy of src // MOD: dest // EFF: place a copy of src in dest { .... } The type signature has a new piece to it: the "const" keyword. Const is a "type qualifier"---something that modifies a type. It means "you cannot change this value once you have initialized it". When you have pointers, of course, there are two things you might change: the value of the pointer, and the value of the object to which the pointer points. Either (or both) can be made unchangeable: const T *p; "T" (the pointed-to object) cannot be changed T *const p; "p" (the pointer) cannot be changed const T *const p; neither can be changed. Strictly speaking, we don't *need* to include the const qualifier here, because the specification promises that we won't modify the source string---it isn't mentioned in the MODIFIES clause. So, why include it? It's useful because, once you add it, you CANNOT change src, even if you do so by mistake---and such a mistake will be caught by the compiler. Bugs that are detected at compile time are among the easiest bugs to fix, and so if we have to have them, those are the kinds we want. Recall that it is certainly possible to change the memory locations pointed to by src, for example, through *another* pointer that points to the same locations. pointers-to-const-T are not the same type as pointers-to-T. You can use a pointer-to-T anywhere you expect a pointer-to-const-T, but NOT vice versa. That's because code that expects a pointer-to-T might try to change the T, but this is illegal for a pointer-to-const-T. However, code that expects a pointer-to-const-T will work perfectly well for a pointer-to-T; it's just gauranteed not to try to change it.
Background image of page 1

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

View Full DocumentRight Arrow Icon
This seems awfully dangerous, because the caller needs to know the length of src before supplying a buffer in dest. This is typically avoided by adding a length argument: void strncpy(char *dest, const char *src, unsigned int length) // REQ src is NUL-terminated string // dest has room for length+1 characters // MOD dest // EFF copy src to dest up to and including the first length characters; terminate dest with NUL { *src++ = *dest++; } *dest = '\0'; } This says that, as long as length is greater than zero *and* *src is non-NUL, copy the the caracter under src to dest, and advance both pointers. If length becomes 0, we've reached the limit, and we have to stop copying. If *src is NUL, we've copied all the "real" characters. In any event, we have to conclude dest with a NUL.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 9

lecture07 - Today: * * * * * * Finish arrays and pointers...

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

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