5 Arrays, Strings, and Structures

# 5 Arrays, Strings, and Structures - 1 CMSC 216 Introduction...

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

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: 2/10/11 1 CMSC 216 Introduction to Computer Systems Lecture 5 Intro. to C, completed, Arrays and Strings & Structures and Unions Jan Plane & Pete Keleher {jplane, [email protected] Array parameters • So array parameters act as if they were passed by reference • If a function modifies elements of an array parameter, the array passed in is modified void function(int a); ... int array[10]; function(array); • Sizes for array parameters are ignored – only types matter – so " void function(int a[12397]); " is equivalent to the above prototype • Generally: You must pass the size as an additional argument 2 CMSC 216 - Wood, Sussman, Herman, Plane 2/10/11 2 Use of symbolic constants • #define preprocessor directive #define name value • All occurrences of name in the source file are replaced by value • Used to define constants for things such as array sizes and other values to improve program maintainability #define ARR_SIZE 3 int main() { int i, a[ARR_SIZE] = {1, 2, 3}; multiply_array(5, a, ARR_SIZE); printf(“[%d”, a[0]); for (i = 1; i < ARR_SIZE; i++) printf(“, %d”, a[i]); printf(“]\n”); return 0; } 3 CMSC 216 - Wood, Sussman, Herman, Plane Strings in C • There is no String type in C • In C, a string is defined as a sequence of characters that is followed by a byte with the value zero – often called: "zero byte", "null byte", "NUL" – represented as the character literal '\0' – "null byte" is NOT the same thing as "null pointer" • Since arrays are contiguous in memory, and char s are all one byte in size, we can use arrays of char s to hold strings • printf() format specifier for strings is %s CMSC 216 - Wood, Sussman, Herman, Plane 4 2/10/11 2 Use of symbolic constants • #define preprocessor directive #define name value • All occurrences of name in the source file are replaced by value • Used to define constants for things such as array sizes and other values to improve program maintainability #define ARR_SIZE 3 int main() { int i, a[ARR_SIZE] = {1, 2, 3}; multiply_array(5, a, ARR_SIZE); printf(“[%d”, a[0]); for (i = 1; i < ARR_SIZE; i++) printf(“, %d”, a[i]); printf(“]\n”); return 0; } 3 CMSC 216 - Wood, Sussman, Herman, Plane Strings in C • There is no String type in C • In C, a string is defined as a sequence of characters that is followed by a byte with the value zero – often called: "zero byte", "null byte", "NUL" – represented as the character literal '\0' – "null byte" is NOT the same thing as "null pointer" • Since arrays are contiguous in memory, and...
View Full Document

## This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

### Page1 / 13

5 Arrays, Strings, and Structures - 1 CMSC 216 Introduction...

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

View Full Document
Ask a homework question - tutors are online