#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    UK
    Posts
    77
    Rep Power
    14

    Invalid initializer error


    Hi,

    I am trying to get the username of the person executing the script but I am getting an error: invalid initializer when I try to compile this with

    either:
    char *userName[] = getenv("LOGNAME");
    or:
    char *userName[] = getlogin();

    Could someone show me the correct way to do this please.

    Thanks
    Jamie
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    247
    Rep Power
    83
    Code:
    char *userName = getenv("LOGNAME");
    char *userName = getlogin();
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    UK
    Posts
    77
    Rep Power
    14
    Originally Posted by spoon!
    Code:
    char *userName = getenv("LOGNAME");
    char *userName = getlogin();
    Both of these give an "initializer element is not constant" error
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    An initializer must be resolved at compile time. Therefore, it cannot include a function call.

    Both of those function calls will need to be made in the code, to be performed at run time.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    UK
    Posts
    77
    Rep Power
    14
    I worked it out,

    I was trying to call these lines outside of the int main() function, after moving them inside this function it worked.

    Jamie
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2007
    Posts
    921
    Rep Power
    536
    The reason is that calling a function is only allowed in another function. A side effect is that a function call is not allowed to be used in the definition of a global variable.

IMN logo majestic logo threadwatch logo seochat tools logo