Solution for Exercise 08_06-allargsp.c
08_06-allargsp.c
#include <stdio.h>
int main( int argc, char **argv )
{
int a,b;
for( a=0; a<argc; a++ )
{
b = 0;
/* the ugly expression is equivalent to
argv[a][b]
*/
while( *(*(argv+a)+b) )
{
putchar( *(*(argv+a)+b) );
b++;
}
putchar('\n');
}
return 0;
}
Output
./a.out
this
is
a
test
Notes
* For the sample output (above), the command line typed is: ./a.out this is a test
* This solution involves using a nested loop. Pointer variable a counts the arguments and represents the address of each string typed at the command prompt. Pointer variable b represents the characters in the string, each one.
* Both pointers, a and b, are addresses. The character in each string is fetched by using this contraption: *(*(argv+a)+b) It represents individual character in each string in a pointer-pointer (char) array.
* The while loop continues until the value fetched from (*(argv+a)+b) is a null character. Remember that (*(argv+a)+b) is an address. Only when it's dereferenced — *(*(argv+a)+b) — is a character fetched.
Copyright © 1997-2025 by QPBC.
All rights reserved
