Thread: Confusing array of char pointer type mismatch

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    15

    Confusing array of char pointer type mismatch

    Hi All,

    I have an array of char pointers:
    Code:
    char *input_args[MAX_ARGS];
    and I have this function:
    Code:
    BOOL parseArgs(char **input_args[], input arg_num);
    I am trying to pass a pointer to this char pointer array like this:
    Code:
    parseArgs(&input_args, args_num);
    But the compiler is complaining:

    Code:
    warning: passing argument 1 of 'parseArgs' from incompatible pointer type ...
    note: expected 'char ***' but argument is of type 'char * (*)[20]'
    Tried a bunch of stuff but nothing works.

    please help,
    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The warning message is self-explanatory. If it is not self-explanatory to you, then you are making the mistake of believing a pointer and an array are interchangeable. They are not.

    In the caller, input_args is an array of 20 pointers to char. &input_args is therefore of type "pointer to array of 20 pointers to char". The function, on the other hand, is expecting a "pointer to a pointer to a pointer to char".

    The simplest way to shut the compiler up would be for the caller to define input_args as
    Code:
        char **input_args[MAX_ARGS];
    and to pass input_args rather than &input_args to the function.

    That is only part of your problem though - depending on what parseArgs() is doing, you will need to initialise the elements to something useful (otherwise parseArgs() will simply read or write to lots of random areas of memory). You will need to read the documentation of parseArgs() to work out what initialisation is needed. Your compiler will not diagnose problems related to incorrect initialisation of input_args.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    If it is not self-explanatory to you, then you are making the mistake of believing a pointer and an array are interchangeable.
    Yes, yes, in fact I did think so! I guess I should go over pointers and arrays again.

    Thanks a lot.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Yes, it's easy to get confused when mixing array notation and pointer notation.

    Bascially you want the the function to taking a char ***, a pointer to a pointer which points to a list of pointers. Whilst you can also specify how big the lists are, this tends to cause trouble.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    Oh, I see.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type Mismatch Error
    By sanks85 in forum C Programming
    Replies: 1
    Last Post: 04-01-2013, 01:58 AM
  2. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  3. Type mismatch warning
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 10-11-2006, 10:34 AM
  4. confusing char pointer
    By vaibhav in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 07:45 PM
  5. Replies: 2
    Last Post: 11-22-2001, 12:22 PM