0

Currently having an issue where type casting a gid_t variable return from getgrnam() in c will allow for me to print the variable but I cannot pass it to a function that takes a char *. What would be the proper way to convert a gid_t to char * in c?

Here is example code of the issue where passing to add_util((char *)gid_num) doesn't work but printing it works fine. When passed it causes a segmentation fault but passing it as add_util((char *)"1557") works just fine.

char *group = (char *)"under";

struct group *record;
record = getgrnam(group);

char *gid_num = (char *)record->gr_gid;


printf("%s", gid_num) //works


add_util((char *)gid_num); //segfault
  • Use `sprintf(some_char_array, "%d", (int)record->gr_gid)` etc. You can't cast a number to a string. – Shawn Nov 08 '22 at 20:44

0 Answers0