0

I have to print address of semaphore. I create a semaphore:

sem_t *sem =  sem_open("name", O_CREAT | O_EXCL, SEM_PERMS, INITIAL_VALUE);

I know that sem_open() returns the address of the new semaphore. So I have to print address of this sem. How can I do this? I've tried something like this:

printf("address sem: %d\n",sem);

id does not work cause its sem_t pointer, have I cast it to char*?

Eldragon01
  • 11
  • 2
  • For pointers use `printf("%p", some_pointer_here);` Also take a look here for additional `printf` format specifiers https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm – DNT May 26 '20 at 09:52
  • yea, i ahve to case it to (void*) and than it works, thanks – Eldragon01 May 26 '20 at 10:01
  • 1
    Not really, `printf` format specifier `"%p"` works for any type of pointer, since all pointers in the same architecture are the same size. – DNT May 26 '20 at 10:04
  • `printf("address sem: %p\n", (void*)sem)` is the only correct way. – Jabberwocky May 26 '20 at 11:41

0 Answers0