Receiving Command-Line Arguments

Discussion in 'iOS Development' started by Hazed, Dec 12, 2007.

  1. Hazed

    Hazed Retired Moderator

    Joined:
    Sep 30, 2007
    Messages:
    762
    Likes Received:
    5
    Device:
    iPhone 3G (Black)
    Does anyone know how I would go about getting Command-Line arguments in my app?

    I have noticed that in main.m there is this line:

    Code:
    int main(int argc, char *argv[]) {
    However, how would I access the char array argv from within:

    Code:
    @implementation test
    	-(void)applicationDidFinishLaunching:(id) unused;
    	{
    	}
    @end
    If anyone has any code samples I would be very greatful.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I don't understand your main function call. Using a pointer to the array would require **argv while simply creating an array would be argv[][], as it is a two dimensional array.

    Unless I'm mistaking, what you are doing is creating a pointer to a one dimensional array, and I'm not sure how your program would react to that. Also, argv++ would increment the location in memory by one space and completely mess things up for you (also argc--). The better thing to do would be to create pointer to each of the elements of the array, hence char **argv.
    Code:
    int main(int argc, char *argv[])  //I'm not even sure this would work
    int main(int argc, char **argv) //is a better idea.
    Although it si early, and I could be wrong.


    But to answer your question, argv is just an integer, and argv is just a two dimensional array of characters.
    Inside of main (because those are local variables confined to main none of your other functions can access them), just use print out argc, simple enough, and create two for loops nested within each other to display the values of argv. Have the first loop increment from the first dimension to the second, while the second loop will increment for the actual length of each dimension.
    (for a lack of better words)
  3. Hazed

    Hazed Retired Moderator

    Joined:
    Sep 30, 2007
    Messages:
    762
    Likes Received:
    5
    Device:
    iPhone 3G (Black)
    The main function call was pre generated by winchain template builder and my apps work fine so it seems.

    My question though is how would I pass the values in the array argv to my applicationDidFinishLaunching in the test class.

    I can see no way of doing it easily so I thought I would ask here

    Please Register or Log in to view images



    Edit: On a side note, **argv would be a pointer to a pointer of argv, not a pointer to a 2d array of argv. Or so I read. Also why is it that you are making it 2D? argv should be a one dimensional char array should it not?
  4. stigmata

    stigmata New Member

    Joined:
    Oct 12, 2007
    Messages:
    14
    Likes Received:
    0
    Device:
    iPod touch
    I know this thread's a bit old, but no one ever answered his question, and I'm bored.

    Why not create a constructor for your test class that takes argc and argv and stores them as instance variables? Problem solved, right? I don't know Objective-C (yet) but from what I can tell, nothing wrong with that.

Share This Page