




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 3
! Similar to standard classes in Java
! Printf takes a variable number of arguments ! First argument is formatting string ! Tells printf what values to expect ! Remaining arguments correspond to specifications in formatting string ! Specifiers include ! %d : integer (as decimal) ! %s : string ! %c : single character ! %f : floating point number ! Specifiers may be modified ! %10d : decimal using at least 10 characters ! %-20s : string using at least 20 characters, left justified
int x = 10; char *s = “Ethan”
printf (“Hello, %s!\n”, s); printf (“x is %d.\n”, x); printf (“’%8s’, x = %d\n”, s, x); printf (“’%-8s’ on left.\n”, s);
Hello, Ethan! x is 10. ‘ Ethan’, x = 10 ‘Ethan ‘ on left.
CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 7
! Standard input is keyboard ! Standard output is terminal screen
! Change standard input with < inputfile ! Change standard output with > outputfile ! May do both
asgn4.pl < plaintext > compressedfile ! This uses the file plaintext rather than keyboard for input ! This sends standard output to compressedfile
! Integers: char, short, int, long ! Floating point: float, double
! Location in memory where the value is actually stored
! Similar to a reference in Java
int *x; ! This says that x is a pointer to an integer
int value = 5; x = &value;
n = x; / will set n = number stored at location x */
CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 9
! May declare a pointer to any type in C ! Includes built-in types! ! May do pointer arithmetic int *p = &x; y = *(p+1) ! This sets y = value in integer following x in memory ! *(p+1) means “add the size of what p points to to p, and dereference it” ! No type checking… ! May assign to dereferenced pointer *p = z; ! Doesn’t modify p! ! Modifies what p points to
x
p
y
z
int arr[8]; ! This declares an array of 8 integers ! Memory allocated for it, but not initialized
! No bounds checking: arr[51] won’t signal an error
0 z
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7]
CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 13
! Don’t modify strings initialized the second way!
! A structure is a mechanism for grouping values together ! Similar to class in Java ! No associated methods! ! May contain builtin types as well as pointers and other structures ! Refer to elements of a structure with “.” notation ! Different notation for referring to elements of a structure being pointed to ! Useful for building complex data structures ! Pointers may refer to memory used for a structure ! How is memory allocated?
struct treenode { int strLength; char s[8]; struct treenode *l; struct treenode *r; };
struct treenode root; root.strLength = 5; // Copy 5 characters into s bcopy (value, s, 5);
! nodep = &root;
! (*p).strLength is equivalent to p->strLength
! Library call malloc ! Compute necessary memory size using sizeof function ! Works on builtin types, too ! May modify the value
struct treenode { int strLength; char s[8]; struct treenode *l; struct treenode *r; };
struct treenode root; struct treenode *nodep; root.strLength = 5; // Copy 5 characters into s bcopy (value, s, 5); p->strLength = 6; bcopy (nValue, p->s, 6); nodep = (struct treenode *) malloc (sizeof (struct treenode); root.l = nodep;