int main( int argc, char *argv[] ) { .... } |
|
char *x[ ] is the declaration of a variable that was defined as: char *x[ N ] Let's analyze this definition step by step: char x ; // x is a char variable char *x; // *x is a char variable // ==> x is a reference to a char variable // ("char *" is also the type of a string !) char *x[10]; // x is an array of 10 reference variables // -- each variable x[i] is a ref. to a char var. // ==> x is an array of strings !!! |
#include <stdio.h> int main(int argc, char *argv[]) { int i; for ( i = 0; i < argc; i++ ) printf("argv[%d] = %s\n", i, argv[i] ); } |
Sample output:
a.out lkls kdlask l daskldsa argv[0] = a.out argv[1] = lkls argv[2] = kdlask argv[3] = l argv[4] = daskldsa |
How to run the program:
|
#include <stdio.h> int main(int argc, char *argv[]) { int i, s, n; s = 0; for ( i = 1; i < argc; i++ ) { n = atoi( argv[i] ) ; // convert string (ASCII code) to int code s = s + n; printf("s = %d\n", s); } printf("\nFinal s = %d\n", s); } |
Sample output:
a.out 1 2 3 4 5 6 s = 1 s = 3 s = 6 s = 10 s = 15 s = 21 Final s = 21 |
How to run the program:
|
|
|
char *s[10] ; // s = array of 10 pointers to string variables |
Using the notation explained above, we have:
|
#include <stdio.h> int main(int argc, char **argv ) { int i; for ( i = 0; i < argc; i++ ) printf("argv[%d] = %s\n", i, argv[i] ); } |
Sample output:
a.out lkls kdlask l daskldsa argv[0] = a.out argv[1] = lkls argv[2] = kdlask argv[3] = l argv[4] = daskldsa |
How to run the program:
|
|
int main(int argc, char **a ) { printf("First 4 chars of arg 0: "); putchar( * ( *(a + 0) + 0 ) ); putchar( * ( *(a + 0) + 1 ) ); putchar( * ( *(a + 0) + 2 ) ); putchar( * ( *(a + 0) + 3 ) ); putchar( '\n' ); printf("First 4 chars of arg 1: "); putchar( * ( *(a + 1) + 0 ) ); putchar( * ( *(a + 1) + 1 ) ); putchar( * ( *(a + 1) + 2 ) ); putchar( * ( *(a + 1) + 3 ) ); putchar( '\n' ); printf("First 4 chars of arg 2: "); putchar( * ( *(a + 2) + 0 ) ); putchar( * ( *(a + 2) + 1 ) ); putchar( * ( *(a + 2) + 2 ) ); putchar( * ( *(a + 2) + 3 ) ); putchar( '\n' ); } |
Sample output:
a.out Hello World First 4 chars of arg 0: a.ou First 4 chars of arg 1: Hell First 4 chars of arg 2: Worl |
How to run the program:
|
*( p + i ) ≡ p[i] |
Simplify:
* ( *(a + i) + j ) ≡ * ( a[i] + j ) // Look at a[i] as ONE SINGLE variable ≡ a[i][j] |
int main(int argc, char **a ) { if ( argc < 3 ) { printf("Needs 2 arguments !\n"); exit(1); } printf("First 4 chars of arg 0: "); putchar( * ( a[0] + 0 ) ); putchar( * ( a[0] + 1 ) ); putchar( * ( a[0] + 2 ) ); putchar( * ( a[0] + 3 ) ); putchar( '\n' ); printf("First 4 chars of arg 0: "); putchar( a[0][0] ); putchar( a[0][1] ); putchar( a[0][2] ); putchar( a[0][3] ); putchar( '\n' ); printf("First 4 chars of arg 1: "); putchar( a[1][0] ); putchar( a[1][1] ); putchar( a[1][2] ); putchar( a[1][3] ); putchar( '\n' ); .... } |
How to run the program:
|
|
char a[3][4]; char **b; int main(int argc, char **argv ) { a[2][3] = 'a'; b[2][3] = 'a'; } |
Compile with: gcc -S args6.s and examine the assembler output:
(%rip is the Intel register that points to the data segment) a[2][3] = 'a': movb $97, a+11(%rip) // 'a' = ASCII 97 // Access the addres a + 11 // Notice: 11 = 2*4 + 3 // I.e.: a[2][3] = 97 ('a') b[2][3] = 'a': movq b(%rip), %rax addq $16, %rax // b + 2*8 (64 bit machine) movq (%rax), %rax // %rax = *b[2] (what I called help) addq $3, %rax // Address help + 3 movb $97, (%rax) // *help[3] = 'a' |
How to run the program:
|