1

Good day people!

The context is resize program in problem set 4, however, the question is general.

Following is how the main function begins for the relevant program: int main(int argc, char *argv[])

Following is how you run the same relevant program: ./resize 22 help.c me.c

The main function takes in command line arguments and stores them with char *argv[]. char *argv[] is an array that will store character pointer variables, each of which will point to the first character of one of the command line arguments entered when running the program.

You have 'argv' (array name) which is a pointer constant pointing to the first RAM address corresponding to the first byte of the contiguous block of RAM made use of for storing the character pointer variables that will each point to the first character (by memory address) of one of the command line arguments entered when running the program.

I am confused at how to access each of the [system null terminated] strings entered as command line arguments i.e. ./resize\0 22\0 help.c\0 me.c\0

Per my understanding, I would have thought that to access any of the stored and null terminated strings, you would have to 'double dereference'.

E.g. argv[0] is the same as *(argv + 0) which would take us to a RAM address that is the RAM address of a character pointer variable, itself storing another RAM address that would take you to the first character of ./resize\0

So to get to the first character of ./resize\0, you would have to double dereference e.g. *(argv[0]) or **(argv + 0) - but from all that I have seen, argv[0] alone is sufficient to take you all the way to the first character of ./resize\0

PLEASE can someone explain to me what I am missing?

1 Answer 1

0

Remember, you've declared *argv[] which is an array of pointers (or, in simpler language, an array of strings). Just like with any other array, you access each element (each string) by its index value, which is simple to access using square bracket notation.

So, if you run ./resize 22 help.c me.c then:

argc = 4  
argv[0] = "./resize"
argv[1] = "22"
argv[2] = "help.c"
argv[3] = "me.c"

Now, you can access the individual chars in one of those, like this, getting the chars in argv[2]:

argv[2][0] = 'h'
argv[2][1] = 'e'
argv[2][2] = 'l'
argv[2][3] = 'p'
argv[2][4] = '.'
argv[2][5] = 'c'
argv[2][6] = '/0' (the null char signifying end of string)

You must have done something similar in your Vigenere program, when you accessed the individual chars in the keyword from argv[1].

If this is still confusing, I'd recommend rewatching the part of Week 2's lecture that dealt with command line arguments (and downloading the sample code) or watching Doug's Command Line Arguments short video.

If you are asking how to do this without using square bracket notation, then

argv[0] = "./resize"  or  *argv
argv[1] = "22"        or  *(argv + 1)
argv[2] = "help.c"    or  *(argv + 2)
argv[3] = "me.c"      or  *(argv + 3)

and then, to access individual chars:

argv[2][0] = 'h'  or   **(argv + 2)
argv[2][1] = 'e'  or   *(*(argv + 2) + 1)

I think you might see how square bracket notation is easier. :)

1
  • Thank you very much for taking the time to advise.
    – CTu
    Aug 20, 2017 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .