CH 15 Flashcards

1
Q

Line 1

A

1 #include

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Line 3

A

3 int main( int argc, char * argv[])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Line 4-10
Create two arrays: 
ages storing some int data, 
and names storing an array of
strings.
A
4 { 
5 // create two arrays we care about 
6 int ages[] = { 23 , 43 , 12 , 89 , 2 }; 
7 char * names[] = { 
8 "Alan" , "Frank" , 
9 "Mary" , "John" , "Lisa" 
10 };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Line 12-14

These are some variables for our for-loops later.

A

12 // safely get the size of ages
13 int count = sizeof (ages) / sizeof ( int );
14 int i = 0 ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Line 16-21
This is just looping through the two arrays and printing how old each person is. This
is using i to index into the array.

A
16 // first way using indexing 
17 for (i = 0 ; i < count; i ++ ) { 
18 printf( "%s has %d years alive.\n" , names[i], ages[i]); 
19 } 
20
21 printf( "---\n" );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Line 23-25
Create a pointer that points at ages.
Create a pointer that points at names.

A

23 // set up the pointers to the start of the arrays
24 int * cur_age = ages;
25 char ** cur_name = names;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Line 27-33
Loop through ages and names but use the pointers plus an offset of i instead.
Writing *(cur_name+i) is the same as writing name[i], and you read it as “the value of
(pointer cur_name plus i).”

A
27 // second way using pointers 
28 for (i = 0 ; i < count; i ++ ) { 
29 printf( "%s is %d years old.\n" , 
30 * (cur_name + i), * (cur_age + i)); 
31 } 
32
33 printf( "---\n" );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Line 35-40

This shows how the syntax to access an element of an array is the same for a pointer and an array.

A

35 // third way, pointers are just arrays
36 for (i = 0 ; i < count; i ++ ) {
37 printf( “%s is %d years old again.\n” , cur_name[i],
cur_age[i]);
38 }
39
40 printf( “—\n” );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

25 char ** cur_name = names;

why does this have two pointers?

A

A char * is already a pointer to char, so that’s just a string.
you need two levels since names is two-dimensional
you need char ** for a pointer to (a pointer to char) type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Line 42-43
Initialize our for-loop by setting cur_name and cur_age to the beginning of the
names and ages arrays.

A
42 // fourth way with pointers in a stupid complex way 
43 for (cur_name = names, cur_age = ages;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Line 44
The test portion of the for-loop then compares the distance of the pointer cur_age
from the start of ages.

A

44 (cur_age - ages) < count; cur_name ++ , cur_age ++ ) {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Line 44
The increment part of the for-loop then increments both cur_name and cur_age
so that they point at the next element of the name and age arrays.

A

44 (cur_age - ages) < count; cur_name ++ , cur_age ++ ) {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Line 45-49
The pointers cur_name and cur_age are now pointing at one element of the
arrays that they work on, and we can print them out using just *cur_name and *cur_age, which
means “the value of wherever cur_name is pointing.”

A
45 printf( "%s lived %d years so far.\n" , * cur_name, *
cur_age); 
46 } 
47
48 return 0 ;
49 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When you type something like ages[i],

A

you’re indexing into the array ages,
using the number that’s held in i to do it.
If i is set to zero then it’s the same as typing ages[0].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

We’ve been calling this number i an index since it’s a location inside ages that we want. It could also be called

A

an address, C, ages is a location in the computer’s memory where all of these integers start. Another way to think of ages is that it’sthe “address of the first integer in ages.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does C think of memory

A

one massive array of bytes.
C does is layer on top
types and
sizes of those types.

17
Q

What does C doing with info

A

Creating a block of memory inside your computer
Pointing the name ages at the beginning of that block
indexing into the block by taking the base address of ages and getting the element that’s i away from there
• Converting that address at ages+i into a valid int of the right size, such that the index works to return what you want: the int at index i

18
Q

simple explanation of a pointer

A

A pointer is simply an address pointing somewhere inside the computer’s memory with a type
specifier so that you get the right size of data with it.

19
Q

There are primarily four useful things you can do with pointers in C code:

A

Ask the OS for a chunk of memory and use a pointer to work with it. This includes strings and something you haven’t seen yet, structs.

Pass large blocks of memory (like large structs) to functions with a pointer, so you don’t have to pass the whole thing to them.

Take the address of a function, so you can use it as a dynamic callback.
Scan complex chunks of memory, converting bytes off of a network socket into data structures or parsing files.

For nearly everything else, you might see people use pointers when they should be using arrays.

20
Q

A pointer of type named ptr

A

type *ptr

21
Q

The value of whatever ptr is pointed at

A

*ptr

22
Q

The value of (whatever ptr is pointed at plus i)

A

*(ptr + i)

23
Q

The address of thing

A

&thing

24
Q

A pointer of type named ptr set to the address of thing

A

type *ptr = &thing

25
Q

Increment where ptr points

A

ptr++