Thread: argv weirdness

  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 02:36 AM.

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

    EDIT - argv is an array of strings, not characters.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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
    28,413
    Note that argv[argc] is a null pointer, hence if argc == 1, argv[1] is a null pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,059
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 06: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, 03:43 PM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM