C-Review (2)

C-Review (2) - C-Programming Review Computer Science...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida C-Programming Review COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
C-Programming Review page 2 C-Programming Review POINTERS
Background image of page 2
C-Programming Review page 3 Review of pointers A pointer is just a memory location. A memory location is simply an integer value, that we interpret as an address in memory. The contents at a particular memory location are just a collection of bits – there’s nothing special about them that makes them ints , chars , etc. How you want to interpret the bits is up to you. Is this. .. an int value? ... a pointer to a memory address? ... a series of char values? 0xfe4a10c5
Background image of page 3

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

View Full DocumentRight Arrow Icon
C-Programming Review page 4 Review of pointer variables A pointer variable is just a variable, that contains a value that we interpret as a memory address. Just like an uninitialized int variable holds some arbitrary “garbage” value, an uninitialized pointer variable points to some arbitrary “garbage address” char *m; (char *) m
Background image of page 4
C-Programming Review page 5 How can you test whether a pointer points to something meaningful? There is a special pointer value NULL , that signifies “pointing to nothing”. You can also use the value 0 . char *m = NULL; ... if (m) { . .. safe to follow the pointer . .. } Here, m is used as a Boolean value If m is “false”, aka 0 , aka NULL , it is not pointing to anything Otherwise, it is (presumably) pointing to something good Note: It is up to the programmer to assign NULL values when necessary
Background image of page 5

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

View Full DocumentRight Arrow Icon
C-Programming Review page 6 Indirection operator * Moves from address to contents char *m = dog ; char result = *m; m gives an address of a char *m instructs us to take the contents of that address result gets the value d (char *) m d (char) o (char) g (char) NUL (char) (char) result d
Background image of page 6
C-Programming Review page 7 Address operator & Instead of contents, returns the address char *m = dog , **pm = &m; pm needs a value of type char ** Can we give it *m ? No – type is char Can we give it m ? No – type is char * &m gives it the right value – the address of a char * value (char *) (char **) m pm d (char) o (char) g (char) NUL (char)
Background image of page 7

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

View Full DocumentRight Arrow Icon
C-Programming Review page 8 Pointer arithmetic C allows pointer values to be incremented by integer values char *m = dog ; char result = *(m + 1); m gives an address of a char (m + 1) gives the char one byte higher *(m + 1) instructs us to take the contents of that address result gets the value o (char *) m d (char) o (char) g (char) NUL (char) (char) result o
Background image of page 8
C-Programming Review page 9 Pointer arithmetic A slightly more complex example: char *m = dog ; char result = *++m; m gives an address of a char ++m changes m, to the address one byte higher, and returns the new address *++m instructs us to take the contents of that location result gets the value o (char *) m d (char) o (char) g (char) NUL (char) (char) result o
Background image of page 9

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

View Full DocumentRight Arrow Icon
C-Programming Review page 10 Pointer arithmetic How about multibyte values?
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

C-Review (2) - C-Programming Review Computer Science...

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

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