Docsity

C Programming: Understanding Arrays, Structs, and Pointers in C - UC Santa Cruz CMPS 12B, Study notes of Computer Science

Partial preview of the text

Download C Programming: Understanding Arrays, Structs, and Pointers in C - UC Santa Cruz CMPS 12B and more Study notes Computer Science in PDF only on Docsity!

Arrays and Structs and Pointers, Oh My!

Programming in C

! Input and output

! Using printf

! Standard input and output

! Pointers

! Arrays

! Structures

! Combining these things together…

CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 3

Printing in C

! C has no “built-in” function to print out values

! Instead, printing is done via library calls

! A library is a collection of function calls that someone

else wrote and has provided

! Similar to standard classes in Java

! Standard C library is called libc

! Standard printing function in C is printf

! Prints everything—numbers, strings, etc.

! May be complex to use…

Using printf

! 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 & output

! In Unix, there’s a standard input stream and standard output

stream

! By default

! Standard input is keyboard ! Standard output is terminal screen

! These can be changed!

! Change standard input with < inputfile ! Change standard output with > outputfile ! May do both

! Example:

asgn4.pl < plaintext > compressedfile ! This uses the file plaintext rather than keyboard for input ! This sends standard output to compressedfile

Pointers

! C has many basic types

! Integers: char, short, int, long ! Floating point: float, double

! Each value has an address

! Location in memory where the value is actually stored

! C allows the program to get this value with a pointer

! Similar to a reference in Java

! Pointers are declared like this:

int *x; ! This says that x is a pointer to an integer

! Pointers can be assigned like this:

int value = 5; x = &value;

! Pointers can be used (dereferenced) like this:

n = x; / will set n = number stored at location x */

CMPS 12B, UC Santa Cruz Arrays and Structs and Pointers, Oh My! 9

More about pointers

! 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

Address Memory

x

p

y

z

What about arrays?

! Arrays are declared:

int arr[8]; ! This declares an array of 8 integers ! Memory allocated for it, but not initialized

! Arrays are referenced:

arr[4] = 5;

z = arr[4];

! No bounds checking: arr[51] won’t signal an error

! Not easy to find the length

of an array

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

Strings in C

! Strings in C are simply arrays of characters

! Strings must be terminated by NUL (\0)

! This makes it impossible to include \0 in a string…

! Since arrays can’t be resized, nor can strings!

! They can be copied onto one another, though

! Strings can be declared like this:

char s1[50];

char *s2 = “test”;

! Now, s2 points to an array of at least 5 characters

(including the trailing \0)

! Some smart compilers may share storage for multiple

strings initialized to “test”…

! Don’t modify strings initialized the second way!

Structures in C

! 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);

Pointers and structures

! Pointers may point at

structures

! nodep = &root;

! Elements referred to with

“->” notation

! (*p).strLength is equivalent to p->strLength

! How is space allocated?

! 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;