CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

But what is argv?

If we read argv's definition, from right to left, it's

"an array of pointers to characters"

Or, try cdecl.org

While we typically associate argv with strings, we remember that C doesn't innately support strings. It's only by convention or assumption that we may assume that each value of argv[i] is a pointer to something that we'll treat as a string.

In the previous example, we print "from" the pointer. Alternatively, we can print every character in the arguments:

#include <stdio.h>

int main(int argc, char *argv[])
{
    for(int a=0 ; a < argc ; ++a) {
        printf("%i: ", a);

	for(int c=0 ; argv[a][c] != '\0' ; ++c)  {  
            printf("%c", argv[a][c] );
        }
        printf("\n");
    }
    return 0;
}

The operating system actually makes argv much more usable, too:

  • each argument is guaranteed to be terminated by a null-byte (because they are strings), and
  • the argv array is guaranteed to be terminated by a NULL pointer.

 


CITS2002 Systems Programming, Lecture 18, p2, 2nd October 2023.