Thread: Declaring an array within a function's argument

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Question Declaring an array within a function's argument

    Hello,

    I have a function
    Code:
    int exec_program(char * arguments[])
    {
    ...
    }
    I can call it like this without a problem:
    Code:
    char * uselessvariable[] = {"/bin/echo", "Testing", NULL};
    exec_program(uselessvariable);
    However I get an error if I try to compile it like this:
    Code:
    exec_program({"/bin/echo", "Testing", NULL});
    Do you know how, in c, I can put this array inside of the argument in one line without having to name a new variable name?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    A braced list of pointers like that could be any number of things, e.g. a struct of three void pointers. So you need to tell the compiler what it is with a cast, like so:

    Code:
    exec_program((char * []){"/bin/echo", "Testing", NULL});

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Quote Originally Posted by smokeyangel View Post
    A braced list of pointers like that could be any number of things, e.g. a struct of three void pointers. So you need to tell the compiler what it is with a cast, like so:

    Code:
    exec_program((char * []){"/bin/echo", "Testing", NULL});
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-24-2013, 10:38 PM
  2. Problem in declaring array size inside a function
    By dpitz in forum C Programming
    Replies: 14
    Last Post: 04-27-2012, 04:17 PM
  3. Array pointer as function argument?
    By mtn_student in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 03:25 PM
  4. Error declaring default argument to friend function
    By Sebastiani in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2009, 01:52 AM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM