pointers, arrays, char**, i don't know...

This is a discussion on pointers, arrays, char**, i don't know... within the C Programming forums, part of the General Programming Boards category; I have been trying a number of ways to copy char *argv[]. I need to parse the command line and ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    pointers, arrays, char**, i don't know...

    I have been trying a number of ways to copy char *argv[].
    I need to parse the command line and call different functions with a subset of the main argument list.

    Code:
    /* the objective is to copy argv into another array */
    /* eventually, for example, copy the 3rd, 4th, 5th argv into 1st, 2nd 3rd arguments */
    
    int main (int argc, char *argv[])
    {
        int j;
        char * arguments[30];  /* max argument size */
        
        for (j = 1; j < argc; j++)
        {
            strcpy (arguments[j], argv[j]);  /* compiles, but get an unhandled exception here */
        }
    }
    Code:
    int main (int argc, char *argv[])
    {
        int j;
        char ** arguments;  
        
        for (j = 1; j < argc; j++)
        {
            strcpy (arguments[j], argv[j]);  /* Run-Time Check Failure #3 - The variable 'arguments' is being used without being initialized. */
        }
    }
    Any direction you can provide is much appreciated.

    Thanks,
    -dog

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Try say
    Code:
    char arguments[30][100];
    For your other attempts, you have to use malloc to allocate sufficient space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Quote Originally Posted by Salem View Post
    Try say
    Code:
    char arguments[30][100];
    For your other attempts, you have to use malloc to allocate sufficient space.
    Thanks. But, then how do I pass the arguments?
    Code:
    void my_function(int argc, char *argv[])
    {
    /*      ... etc., etc., etc.   */
    }
    
    int main (int argc, char *argv[])
    {
        int arg_count = 0;
        char * arguments[30][30];  /* max argument size */
        
        for (int j = 1; j < argc; j++)
        {
            strcpy (arguments[j], argv[j]); 
            arg_count++'
        }
    
        my_function(arg_count, **arguments);   
    /* argument of type "char" is incompatible with parameter of type "char **"  */
       
        my_function(arg_count,  *arguments);
    /* argument of type "char *" is incompatible with parameter of type "char **" */
    
        my_function(arg_count,  arguments);
    /* argument of type "char (*)[30]" is incompatible with parameter of type "char **" */
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    For one thing, compare the code Salem provided (ignoring the array length difference)
    Code:
    char arguments[30][100];
    with yours
    Code:
    char * arguments[30][30];  /* max argument size */
    You're also going to need another argument to your function.
    Code:
    void my_function(int argc, char *argv[], char my_arguments[][30])

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Quote Originally Posted by rags_to_riches View Post
    For one thing, compare the code Salem provided (ignoring the array length difference)
    Code:
    char arguments[30][100];
    with yours
    Code:
    char * arguments[30][30];  /* max argument size */
    Yeah, crud -- that was a typo. Should read:
    Code:
    char arguments[30][30];  /* max argument size */
    Quote Originally Posted by rags_to_riches View Post
    You're also going to need another argument to your function.
    Code:
    void my_function(int argc, char *argv[], char my_arguments[][30])
    There's not a way to call this?
    Code:
    void my_function(int argc, char *argv[])
    Thanks for your help.
    -dog

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You really need to read a tutorial about pointers and arrays in C before continuing this exercise. You seem to be confused about what a pointer is, what an array is and when is an array treated as a pointer to the first element.

    I suggest you start here:

    Cprogramming.com Tutorial: Pointers

    and then here:

    Cprogramming.com FAQ > Pointers And Arrays (intermediate)

    As for you exercise if what you are trying to do is to call different functions depending on what the argument passed to the program is , then you probably need a something like a switch statement in main to branch away and call the appropriate function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you do not need to alter the contents of the arguments, just which ones you have, then you just need to assign:
    Code:
    /* the objective is to copy argv into another array */
    /* eventually, for example, copy the 3rd, 4th, 5th argv into 1st, 2nd 3rd arguments */
    
    int main (int argc, char *argv[])
    {
        int j;
        char * arguments[3];  /* I appear to want three arguments */
        
        for (j = 2; j < 5; j++)
        {
            arguments[j-2] = argv[j];
        }
    }
    If you intend to make a copy so that you can change things, you will need to acquire memory and then use strcpy to fill the memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointers and arrays
    By Scramble in forum C Programming
    Replies: 3
    Last Post: 06-30-2010, 04:17 AM
  2. Pointers & char arrays
    By lautarox in forum C Programming
    Replies: 11
    Last Post: 05-13-2009, 08:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM
  5. char arrays, pointers, etc, could use help badly
    By Nakeerb in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2002, 10:20 PM

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