Arguments and pointers
This commit is contained in:
		
							parent
							
								
									1cf4a8c67e
								
							
						
					
					
						commit
						48249f9fee
					
				| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
    printf("Given %d arguments.\n", argc);
 | 
			
		||||
 | 
			
		||||
    int index = 0;
 | 
			
		||||
    while (index < argc) {
 | 
			
		||||
        printf("Argument %d: %s\n", index, argv[index]);
 | 
			
		||||
        index = index + 1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										62
									
								
								README.md
								
								
								
								
							
							
						
						
									
										62
									
								
								README.md
								
								
								
								
							| 
						 | 
				
			
			@ -143,7 +143,65 @@ printf("Hello, world!\n");
 | 
			
		|||
```
 | 
			
		||||
 | 
			
		||||
This calls the `printf` function, which `print`s a `f`ormatted string, although we're
 | 
			
		||||
not using any of the formatting options here.
 | 
			
		||||
not using any of the formatting options here. The `\n` is a standin for the newline
 | 
			
		||||
character.
 | 
			
		||||
 | 
			
		||||
Congratulations! This is your first C program~
 | 
			
		||||
Congratulations! This is your first C program, and demonstrates that your development
 | 
			
		||||
environment is working.
 | 
			
		||||
 | 
			
		||||
# Pointers and Looping
 | 
			
		||||
 | 
			
		||||
In a file called `01-arguments.c`, write:
 | 
			
		||||
 | 
			
		||||
```c
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
    printf("Given %d arguments.\n", argc);
 | 
			
		||||
 | 
			
		||||
    int index = 0;
 | 
			
		||||
    while (index < argc) {
 | 
			
		||||
        printf("Argument %d: %s\n", index, argv[index]);
 | 
			
		||||
        index = index + 1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This is pretty similar, but has a few more complications.
 | 
			
		||||
 | 
			
		||||
```c
 | 
			
		||||
printf("Given %d arguments.\n", argc);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Here, `%d` means "replace this placeholder with the first argument after the string,
 | 
			
		||||
which should be an integer". `d` stands for digit.
 | 
			
		||||
 | 
			
		||||
```c
 | 
			
		||||
int index = 0;
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This is the declaration of a variable "index", which will be used to keep track of our
 | 
			
		||||
place in the array.
 | 
			
		||||
 | 
			
		||||
```c
 | 
			
		||||
while (index < argc) {
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This means run the following code until the `index` variable becomes greater than or equal
 | 
			
		||||
to `argc`, the number of command line arguments.
 | 
			
		||||
 | 
			
		||||
```c
 | 
			
		||||
printf("Argument %d: %s\n", index, argv[index]);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
This line has a few new things in it; `%s` is the placeholder for strings, and `argv[index]`
 | 
			
		||||
is taking looking at the `index`th element in `argv`, an array of strings.
 | 
			
		||||
 | 
			
		||||
Or, you can look at it a different way: `argv` is a `char**`, a `*` (pointer) to a `*` (pointer)
 | 
			
		||||
to a `char`. A "pointer" is simply an **unsigned integer** which represents, instead of a
 | 
			
		||||
number, a location in memory.
 | 
			
		||||
 | 
			
		||||
So, `char** argv` is the memory location of the first element in a list of pointers, each
 | 
			
		||||
of which is the memory location of the first element in a list of `char`s. Those lists of
 | 
			
		||||
`char`s all end with `\0`, the 0 byte, and represent the text of the program's command
 | 
			
		||||
line arguments.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue