Art_of_Programming_Contest_Part3

# Art_of_Programming_Contest_Part3 - CHAPTER 3 PROGRAMMING IN...

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

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 43 and the sequence y = &x[0]; y++; leaves y pointing at x[1]. Let's use pointers in a function length that computes how long a character array is. Remember that by convention all character arrays are terminated with a `\0'. (And if they aren't, this program will blow up inevitably.) The old way: length(s) char s[ ]; { int n; for( n=0; s[n] != '\0'; ) n++; return(n); } Rewriting with pointers gives length(s) char *s; { int n; for( n=0; *s != '\0'; s++ ) n++; return(n); } You can now see why we have to say what kind of thing s points to -- if we're to increment it with s++ we have to increment it by the right amount. The pointer version is more efficient (this is almost always true) but even more compact is for( n=0; *s++ != '\0'; n++ ); The `*s' returns a character; the `++' increments the pointer so we'll get the next character next time around. As you can see, as we make things more efficient, we also make them less clear. But `*s++' is an idiom so common that you have to know it. Going a step further, here's our function strcopy that copies a character array s to another t. strcopy(s,t) char *s, *t; { while(*t++ = *s++); } CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 44 We have omitted the test against `\0', because `\0' is identically zero; you will often see the code this way. For arguments to a function, and there only, the declarations char s[ ]; char *s; are equivalent -- a pointer to a type, or an array of unspecified size of that type, are the same thing. Function Arguments Look back at the function strcopy in the previous section . We passed it two string names as arguments, then proceeded to clobber both of them by incrementation. So how come we don't lose the original strings in the function that called strcopy? As we said before, C is a ``call by value'' language: when you make a function call like f(x), the value of x is passed, not its address. So there's no way to alter x from inside f . If x is an array (char x[10]) this isn't a problem, because x is an address anyway, and you're not trying to change it, just what it addresses. This is why strcopy works as it does. And it's convenient not to have to worry about making temporary copies of the input arguments. But what if x is a scalar and you do want to change it? In that case, you have to pass the address of x to f , and then use it as a pointer. Thus for example, to interchange two integers, we must write flip(x, y) int *x, *y; { int temp; temp = *x; *x = *y; *y = temp; } and to call flip , we have to pass the addresses of the variables: flip (&a, &b); Which interchange two integers. CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 45 The Switch Statement ; Break ; Continue The switch statement can be used to replace the multi-way test we used in the last example. When the tests are like this: if( c == 'a' ) ......
View Full Document

## This document was uploaded on 08/10/2011.

### Page1 / 20

Art_of_Programming_Contest_Part3 - CHAPTER 3 PROGRAMMING IN...

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

View Full Document
Ask a homework question - tutors are online