02-pointersNarrays

02-pointersNarrays -...

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

View Full Document Right Arrow Icon
CPE 357 - Systems Programming Spring 2009, Day 2 Pointers and Arrays Last Modified: Sat Jan 10 12:44:11 PST 2009 Resources: args.c off.c Recap: used pointers like Java used references? scanf -- have to remember to give an address, not a scalar variable Sometimes, used to change call-by-value into call-by-reference Noticed, perhaps, that array[0] is like *arrayp? Or even arrayp[0] More Arrays are much like (but not completely like) constant pointers to the array contents. But pointers are variables and you can change them. Command line int main(int argc, char **argv) int main(int argc, char *argv[]) << Multi-dimensional array? Assignments int array[12]; int *arrayp; arrayp = array; // because of "pointer generation" arrayp = &array[0]; Pointer Generation from "bare" array or function name. Actual rules are a bit arcane. But this rule of thumb is okay. Usually. Reference array[2] is the same as *(array + 2) Really like it (remember 31[a] from the 12 days program?) suppose int *arrayp = &array[1]; int *arrayq = &array[4];
Background image of page 1

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

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

Unformatted text preview: printf(&quot;%d\n&quot;, arrayq - arrayp); Note: arrayp += 3 makes them equal. The unit is not bytes, but sizeof(*arrayp). BUT: int intp = arrayp; // NOT int* int intq = arrayq; printf(&quot;%d\n&quot;, intq - intp); Undefined! But universally not 3. Expect 3 * sizeof(array[0]) Parameters. prototype: int strcount(char str, char what); but call is strcount(arrayp, ' ') It's okay. arrayp is cast to char, which you can't do with an assignment, only with a parameter. How about int words(char *strp)? words(string) okay? sure. it's cast by assignment, as we did earlier If there's time: Incomplete types Multi-dimensional arrays Why they're not frequently used or seen NOTE: we already saw assignment among pointer types, but that's not portable. What we CAN count on is void *temp; temp = anypointer; anypointer = temp; and anypointer is back as it began. LAB: write the (my)strcmp function. give it string pointers and arrays. vim: set sts=4 sw=4 ai et :...
View Full Document

Page1 / 3

02-pointersNarrays -...

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