Download C Arrays, Strings, More Pointers and more Lecture notes Architecture in PDF only on Docsity!
C Arrays, Strings, More Pointers
Instructor: Steven Ho
Review of Last Lecture
- C Basics
- Variables, Functions, Flow Control, Types, and Structs
- Only 0 and NULL evaluate to FALSE
- Pointers hold addresses
- Address vs. Value
- Allow for efficient code, but prone to errors
- C functions “pass by value”
- Passing pointers circumvents this
sizeof()
6/20/2017 CS61C Su18 – Lecture 2 4
- Integer and pointer sizes are machine dependent—how do we tell?
- Use sizeof() function
- Returns size in bytes of variable or data type name Examples: int x, *y; sizeof(x); // 4 (32-bit int) sizeof(int); // 4 (32-bit int) sizeof(y); // 4 (32-bit addr) sizeof(char); // 1 (always)
sizeof()
6/20/2017 CS61C Su18 – Lecture 2 5
- Acts differently with arrays and structs (to be explained later) - Arrays: returns size of whole array - Structs: returns size of one instance of struct (sum of sizes of all struct variables + padding)
- eg: (^) typedef struct example { char *name; int year; } Song; Song song1; sizeof(song1); // 4 + 4 = 8
Great Idea #1: Levels of
Representation/Interpretation
lw lw x5, 0(x2)x6, 4(x2) sw sw x6, 0(x2)x5, 4(x2)
Higher-Level LanguageProgram (e.g. C)
Program (e.g. RISCV)^ Assembly Language Machine LanguageProgram (RISCV)
Hardware Architecture Description (e.g. block diagrams)
Compiler
Assembler
Interpretation^ Machine
temp = v[k]; v[k] = v[k+1]; v[k+1] = temp;
0000 1001 1100 0110 1010 1111 0101 1010 1111 0101 1000 0000 1001 1100 (^10000110) 1100 0110 1010 1111 0101 1000 0000 0101 1000 0000 1001 1100 0110 1010 (^10011111)
Logic Circuit Description (Circuit Schematic Diagrams)
Implementation^ Architecture
We are here_
Agenda
- C Operators
- Arrays
- Administrivia
- Strings
- More Pointers
- Pointer Arithmetic
- Pointer Misc
Assignment and Equality
- One of the most common errors for beginning C programmers a = b is assignment a == b is equality test
- Comparisons use assigned value
- if (a=b) is true if a≠0 after assignment (b≠0)
Operator Precedence
- Use parentheses to manipulate
- Equality test (==) binds more tightly than logic (&, |, &&, ||) - x&1==0 means x&(1==0) instead of (x&1)==
For precedence/order of execution, see Table 2-1 on p. 53 of K&R
Agenda
- C Operators
- Arrays
- Administrivia
- Strings
- More Pointers
- Pointer Arithmetic
- Pointer Misc
Array Basics
int ar[2]; declares a 2-element integer array (just a block of memory) int ar[] = {795, 635}; declares and initializes a 2-element integer array
ar[num] returns the num th^ element
Accessing an Array
- Array size n : access entries 0 to n-
- Use separate variable for array declaration & array bound to be reused (eg: no hard-coding) - Bad pattern int i, ar[10]; for(i=0; i<10; i++) {...} - Better pattern int ARRAY_SIZE = 10; int i, ar[ARRAY_SIZE]; for(i=0; i<ARRAY_SIZE; i++) {...}
Single source of truth!
Bad Pattern
Better Pattern
Arrays and Pointers
- Arrays are (almost) identical to pointers
- char *string and char string[] are nearly identical declarations
- Differ in subtle ways: initialization, sizeof(), etc.
- Key Concept: An array variable looks like a pointer to the first (0 th^ ) element - ar[0] same as *ar; ar[2] same as *(ar+2) - We can use pointer arithmetic to conveniently access arrays
- An array variable is read-only (no assignment) (i.e. cannot use “ar = ”)
Arrays Stored Differently Than
Pointers
void foo() { int p, a[4], x; p = &x; p = 1; // or p[0] printf("p:%u, p:%u, &p:%u\n",p,p,&p); a = 2; // or a[0] printf("a:%u, a:%u, &a:%u\n",*a,a,&a); } ... (^40)?? 2? 1 ...
**p:1, p:40, &p: a:2, a:24, &a:
K&R: “An array__. name is not a a variable”.
24
?
0 4 8 12 16 20 24 28 32 36 40 44 48 … p x
Arrays and Functions
- Declared arrays only allocated while the scope is valid: char *foo() { char string[32]; ...; return string; }
- An array is passed to a function as a pointer:
int foo(int ar[], unsigned int size) { ... ar[size-1] ... }
BAD
Must explicitly pass the size!
Really int *ar