Week 9.docx

6 Pages
42 Views

Department
Electrical & Computer Engineer
Course Code
ECE 15
Professor
Alon Orlitsky

This preview shows pages 1 and half of page 2. Sign up to view the full 6 pages of the document.
Description
1. Problems with Changing Parameters in a function: a. Recall: Function arguments are passed by value i. When we write a function to call with a parameter, the parameter value is copied into the calling parameter. Thus, the function technically has no access to the address of the original value that was used as a calling parameter, and thus cannot change it. b. Returning Multiple Values i. We cannot return two values with only one function. c. Passing arrays to functions wastes memory and time. 2. Pointers pass their addresses of the values. 3. Pointers 1. Reminder: a. Memory is organized as sequence of bytes, and variables allocated a prescribed number of bytes based on value type. b. Local variables are allocated consecutive/adjacent addresses in memory. c. Each variable has an address in memory, which can be used to access it. b. Address and Dereference: i. Address operator: &a is the address of the variable “a” ii. Deference (indirection) operator: if “a” is an address location, then *a is the variable at address a, or pointed to by “a” 1. Ex: *(1000) is ‘a’. c. If a pointer “p” points to a variable “a”, then *p is a and p is &a. i. To get address, P = &a ii. To follow address, a = *P d. Declaration and Assignment: i. For every value type, there is a type POINTING to each value type. ii. Declaration: 1. “type” *p; a. ex: int *p; b. double *pdbl; c. char *cp; d. Boolean *bp; iii. Assignment: 1. p = &a. Non-pointer types (int for example) can be assigned by us: (for example, int x = 5) 2. For pointers, we don’t know address when it is initialized, hence we ask program to initialize via “&”. a. int *p; p = &x; b. int *p = &x; 3. The difference between *p and p is that if you were to assign a variable to *p (var = *p), it would become the value ASSIGNED into the address that “p” points to. If you were to assign a variable to p (var = p), the variable would become the ACTUALaddress location of the value it is pointing to (e.g. the value location of x is 1000, so *p = x, and p = 1000) iv. declarations.c e. Mechanics: i. int a = 3 int *p, *q p = &a q = p; 1. “p” points to the address of variable “a”, and “q” points the address of “p”, which is the address of “a”. ii. int a = 30 int int *p p = &a y = *p *p = 42; 1. “p” points the address of a, and y becomes the value of the address that “p” points to. “*p = 42” changes the value of the address at variable “a” to 42. iii. Points are not automatically initialized, meaning the address that it points to is not initialized. Thus. *p = x can crash the program. iv. Pointers of different value types cannot be mixed. f. Precedence: i. int i = 3, j = 5, *p = &I, *q = &j, *r; double x; 1. p is equal to &*p a. p == &*p == p == &(*p) = 1 2. **&p is the same as *(*(&p)) 3. r = &x is an error, because they are different value types 4. (7*(*p))/(*q) + 7 = 11 and (7*(*p))/(*q)+7 = 11 a. Pointers are unary operators, thus they take precedence. 5. *(r = &j) *=* p = 15 and *(r = &j) *= (*p) a. remember that “*” means both pointer and multiplication. g. How they solve Shortcomings: i. swap.c ii. Given “a” and “b” representing the numerator and denominator of a fraction a/b, reduce the fraction to its lowest terms, namely cancel all common factors of a and b. For example, 18/24 becomes ¾. 1. reduce.c 2. With this, we are no
More Less
Unlock Document

Only pages 1 and half of page 2 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit