CH 15 Flashcards
(25 cards)
Line 1
1 #include
Line 3
3 int main( int argc, char * argv[])
Line 4-10 Create two arrays: ages storing some int data, and names storing an array of strings.
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 };
Line 12-14
These are some variables for our for-loops later.
12 // safely get the size of ages
13 int count = sizeof (ages) / sizeof ( int );
14 int i = 0 ;
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.
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" );
Line 23-25
Create a pointer that points at ages.
Create a pointer that points at names.
23 // set up the pointers to the start of the arrays
24 int * cur_age = ages;
25 char ** cur_name = names;
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).”
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" );
Line 35-40
This shows how the syntax to access an element of an array is the same for a pointer and an array.
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” );
25 char ** cur_name = names;
why does this have two pointers?
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.
Line 42-43
Initialize our for-loop by setting cur_name and cur_age to the beginning of the
names and ages arrays.
42 // fourth way with pointers in a stupid complex way 43 for (cur_name = names, cur_age = ages;
Line 44
The test portion of the for-loop then compares the distance of the pointer cur_age
from the start of ages.
44 (cur_age - ages) < count; cur_name ++ , cur_age ++ ) {
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.
44 (cur_age - ages) < count; cur_name ++ , cur_age ++ ) {
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.”
45 printf( "%s lived %d years so far.\n" , * cur_name, * cur_age); 46 } 47 48 return 0 ; 49 }
When you type something like ages[i],
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].
We’ve been calling this number i an index since it’s a location inside ages that we want. It could also be called
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 does C think of memory
one massive array of bytes.
C does is layer on top
types and
sizes of those types.
What does C doing with info
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
simple explanation of a pointer
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.
There are primarily four useful things you can do with pointers in C code:
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.
A pointer of type named ptr
type *ptr
The value of whatever ptr is pointed at
*ptr
The value of (whatever ptr is pointed at plus i)
*(ptr + i)
The address of thing
&thing
A pointer of type named ptr set to the address of thing
type *ptr = &thing