argv weirdness

This is a discussion on argv weirdness within the C Programming forums, part of the General Programming Boards category; Hi, I'm experimenting with argv to learn more about how it works for a program I'm writing. I'm working with ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    argv weirdness[SOLVED]

    Hi,

    I'm experimenting with argv to learn more about how it works for a program I'm writing. I'm working with the code below:

    Code:
    #include<stdio.h>
    
    int main( int argc, char **argv)
    {
        char *array = argv[1];
        int i;
        printf( "%d\n", sizeof(array));
        for( i = 0; i < sizeof(array); i++ )
        {
            printf( "%c\n", array[i] );
        }
        return(0);
    }
    The thing that has me is the following behaviour:

    [calef13@bluenode c]$ ./test1 127.0.0.1 1-20
    4
    1
    2
    7
    .
    [calef13@bluenode c]$
    Can anybody here explain why it's stopping at the first 0? I can't for the life of me fathom it. But I suspect I'm doing something undefined. The code above works just fine for a second parameter "1-200". Also if there is a better way to get a string out of argv so it can be compared character by character, let me know.
    Last edited by Calef13; 12-16-2008 at 01:36 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Put the IP address in double quotes.

    EDIT - argv is an array of strings, not characters.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Calef13 View Post
    Hi,

    I'm experimenting with argv to learn more about how it works for a program I'm writing. I'm working with the code below:

    Code:
    #include<stdio.h>
    
    int main( int argc, char **argv)
    {
        char *array = argv[1];
        int i;
        printf( "%d\n", sizeof(array));
        for( i = 0; i < sizeof(array); i++ )
        {
            printf( "%c\n", array[i] );
        }
        return(0);
    }
    Also, your loop construct is wrong. sizeof(array) will most likely always be 4, since a pointer is most always 4 bytes. What you want is to loop based on argc - 1.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    sizeof(array) is not a good test for a terminal condition as Dino pointed out.
    walk along the char array that argv[1] points to until a NULL (string terminator) is seen.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok thanks for all the replies everyone, I understand now, I should have been using strlen() for one. Just for completeness, the code is below.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main( int argc, char **argv)
    {
        char *array = argv[1];
        int i = 0;
        printf( "&#37;s\n", array );
        printf( "%d\n", strlen(array) );
        while( array[i] != '\0' )
        { 
          printf( "%c", array[i]);
          i++;
        }
        getch();
        return 0;
    }
    So argv[1] is just a pointer to c string, nothing special. Python has ruined me :P

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Note that argv[argc] is a null pointer, hence if argc == 1, argv[1] is a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Calef13 View Post
    ok thanks for all the replies everyone, I understand now, I should have been using strlen() for one. Just for completeness, the code is below.
    Neither do you need strlen() nor do you need the variables i and *array (unless you don't want to change what argv[1] is currently pointing to).
    Just increment argv[1] while printing out each character it points to in turn until you reach the end of the string signaled by the NULL character.
    Quote Originally Posted by Calef13 View Post
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main( int argc, char **argv)
    {
        char *array = argv[1];
        int i = 0;
        printf( "%s\n", array );
        printf( "%d\n", strlen(array) );
        while( array[i] != '\0' )
        { 
          printf( "%c", array[i]);
          i++;
        }
        getch();
        return 0;
    }
    So argv[1] is just a pointer to c string, nothing special. Python has ruined me :P
    yes argv[1] is just a pointer to an array of char.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    You can use strlen to find the length of a string. You can print a string with %s.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 05:09 AM
  3. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  4. Argv And Argc
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-11-2001, 02:43 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21