Documente Academic
Documente Profesional
Documente Cultură
Introduction to C
Part 2
CS33 Intro to Computer Systems II–1 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Swapping
CS33 Intro to Computer Systems II–2 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Why “pass by value”?
int main() {
function(2);
printf("%d\n", 2);
} $ ./a.out
void function(int x) { 3
x = 3;
}
CS33 Intro to Computer Systems II–3 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Variables and Memory
What does
int x;
do?
What’s memory?
CS33 Intro to Computer Systems II–4 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Memory
• “Real” memory
– it’s complicated: we discuss it later in the course
– it involves electronics, semiconductors, physics,
etc.
– it’s not terribly relevant at this point
• “Virtual” memory
– the notion of memory as used by programs
– it involves logical concepts
– it’s how you should think about memory (most of
the time)
CS33 Intro to Computer Systems II–5 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
0:
1:
Virtual Memory 2:
3:
4:
5:
4294967294:
4294967295:
CS33 Intro to Computer Systems II–6 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
0:
1:
Memory addresses in C 2:
3:
4:
5:
• In C .
.
- you can get the memory .
address of any variable 134217728:
- just use the operator & 134217729:
a
4
134217730:
134217731:
int main() {
int a = 4; .
printf("%u\n", &a); .
.
}
$ ./a.out
134217728 4294967294:
4294967295:
CS33 Intro to Computer Systems II–7 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
C Pointers
• What is a C pointer?
– a variable that holds an address
• Pointers in C are “typed” (remember the promises)
– pointer to an int
– pointer to a char
– pointer to a float
– pointer to <whatever you can define>
• C has a syntax to declare pointer types
– things start to get complicated …
CS33 Intro to Computer Systems II–8 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
C Pointers
p is a pointer to an int
$ ./a.out
134217728
CS33 Intro to Computer Systems II–9 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
0:
1:
C Pointers 2:
3:
4:
5:
.
.
int main() { .
int *p; 134217728:
int a = 4; 134217729:
a
4
134217730:
p = &a; 134217731:
printf("%u\n", p); 134217732:
134217728
134217733:
} p
134217734:
134217735:
$ ./a.out
134217728 .
.
.
4294967294:
4294967295:
CS33 Intro to Computer Systems II–10 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
C Pointers
CS33 Intro to Computer Systems II–11 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Swapping
CS33 Intro to Computer Systems II–12 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
0:
1:
C Pointers 2:
3:
4:
5:
.
• Dereferencing pointers .
.
– accessing/modifying the value
pointed to by a pointer 134217728:
134217729:
a
4
5
134217730:
int main() { 134217731:
int *p; 134217732:
134217728
134217733:
int a = 4; 134217734:
p
p = &a; 134217735:
printf("%d\n", *p);
*p = *p + 1; .
.
printf("%d\n", *p); .
} $ ./a.out
4294967294:
4 4294967295:
CS33 Intro to Computer Systems II–14 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Swapping
CS33 Intro to Computer Systems II–15 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Quiz 1
CS33 Intro to Computer Systems II–16 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointers and Arrays
a 33
a[0] a[1] a[2] a[6]
p int main() {
int a[7];
int *p;
p = &a[0];
*p = 33;
}
CS33 Intro to Computer Systems II–17 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointer Arithmetic
Pointers can be incremented/decremented
– what this does depends on its type
a 33 167
a[0] a[1] a[2] a[6]
int main() {
p int a[7];
int *p;
p = &a[0];
*p = 33;
*(p+1) = 167;
}
CS33 Intro to Computer Systems II–18 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointer Arithmetic
Pointers can be incremented/decremented
– what this does depends on its type
a
a[0] a[1] a[2] a[6]
p int main() {
int a[7]; Now p and a
int *p; have the
p = &a[0]; same value
}
CS33 Intro to Computer Systems II–19 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointer Arithmetic
Pointers can be incremented/decremented
– what this does depends on its type
a 33 167
a[0] a[1] a[2] a[6]
int main() {
p int a[7];
int *p;
p = a;
*p = 33;
p[1] = 167;
}
CS33 Intro to Computer Systems II–20 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointers and Arrays
CS33 Intro to Computer Systems II–21 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Pointers and Arrays
CS33 Intro to Computer Systems II–22 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Literals and Functions
initialized with a copy
int func(int x) { of the argument
x = x + 4;
return x * 2;
}
int main() {
result = func(2);
printf("%d\n", result);
return 0;
}
CS33 Intro to Computer Systems II–23 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Arrays and Functions
initialized with a copy
int func(int *a, int nelements) { of the argument
// sizeof(a) == sizeof(int *)
int i;
for (i=0; i<nelements-1; i++)
a[i+1] += a[i];
return a[nelements-1];
}
int main() {
int array[50] = ... ;
// sizeof(array) == 50*sizeof(int)
printf("result = %d\n", func(array, 50));
return 0;
}
CS33 Intro to Computer Systems II–24 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Equivalently ...
int func(int a[], int nelements) {
// sizeof(a) == sizeof(int *)
... No need for array size,
since all that’s used is
} pointer to first element
int main() {
int array[50] = ... ;
// sizeof(array) == 50*sizeof(int)
printf("result = %d\n", func(array, 50));
return 0;
}
CS33 Intro to Computer Systems II–25 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Quiz 2
int func(int a[], int nelements) {
int b[5] = {0, 1, 2, 3, 4};
a = b; This program prints:
return a[1];
} a) 0
b) 1
int main() { c) 2
int array[50]; d) nothing: it doesn’t
compile because of a
array[1] = 0;
syntax error
printf("result = %d\n",
func(array, 50));
return 0;
}
CS33 Intro to Computer Systems II–26 Copyright © 2019 Thomas W. Doeppner. All rights reserved.
Quiz 3
int func(int a[], int nelements) {
int b[5] = {0, 1, 2, 3, 4};
a = b; This program prints:
return a[1];
} a) 0
b) 1
int main() { c) 2
int array[5] = {4, 3, 2, 1, 0}; d) 3
func(array, 5);
printf("%d\n", array[1]);
return 0;
}
CS33 Intro to Computer Systems II–27 Copyright © 2019 Thomas W. Doeppner. All rights reserved.