Quick Question: 2D Array with char **

This is a discussion on Quick Question: 2D Array with char ** within the C Programming forums, part of the General Programming Boards category; Quick Question I have some functions who take as parameters an int and and a char **. Basically the work ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Quick Question: 2D Array with char **

    Quick Question
    I have some functions who take as parameters an int and and a char **. Basically the work in the same line of thought as main in c. I pass the number of arguments and the arguments themselves as strings.

    So in my main program I had declared a char ** arguments and I allocated space with malloc and stored the arguments input from the user in there but then I tried to declare it as a char[8][16] because malloc seemed unneeded and I was told that it was probably unneeded to alloacate space in the stack but I get an error now while compiling that the argument passed in the functions is incompatible type. How do I pass a 2d array as a pointer to a function?
    Also I tried to change the parameter to a char[][] but I think that you need the size of the second []
    like so char[][16] right.
    But the think is I dont want to pass a copy of an array of strings or am I being unreasonbale. I know that nowadays an array of string isn't much of space but I think it would be preferable to pass pointers and the thing is that I store the arguments in another function so if I pass a copy I wont succed anything

    the function are like this
    Code:
    int function (int c, char **v)
    and the local variable which I pass is
    Code:
    char arguments[8][16];
    Last edited by Phoenix_Rebirth; 01-29-2009 at 06:12 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A two-dimensional array is not a pointer to pointer - you need a pointer for each of the second dimension elements for a pointer to pointer to act as a two-dimensional array. See my other post in your "Want comments thread" for a suggestion to your SPECIFIC problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    int function (int c, char v[][16]);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    int function (int c, char (*v)[16])

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Ok thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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