0

I have a character array containing my user ID. I need to get a uid_t object so I can look up the username using getpwuid(3).

My command is ./foo 1000 (a user with the name 'bar' and the ID 1000 exists on the system). Here's my code:

int main(int argc, char **argv) {
    struct passwd *user = getpwuid((int) argv[1]);

    printf("%s\n", user->pw_name);
}

The problem with this is, getpwuid(3) only accepts uid_t, and the (int) casting is not working.

I am well aware that I could use getpwnam() and get it from a string, but I can't figure out how to convert my char array to a uid_t so getpwuid(3) accepts it.

n0stalghia
  • 31
  • 1
  • 7
  • You're looking for `atoi(3)`. – iBug Jan 18 '21 at 17:30
  • 1
    Casting here is really wrong, so please, take this as a moment to learn what that casting actually does. C does conversion between only a handful of types when casting, this is the exception not the norm. – tadman Jan 18 '21 at 17:30

0 Answers0