Why does sizeof (void(*)(void)) print 8? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have seen following weird type of syntax on here:
printf("sizeof(void(*)(void)) = %zu\n", sizeof(void(*)(void)));
What is the meaning of void(*)(void)?
What does it do?
When and where should i use it?
Why does sizeof it 8?

Because a function pointer is 8 bytes on that platform.
The expression void(*)(void) is a pointer to a function that takes no arguments and has no return value, so that's the type whose size you're asking for.
So, attacking your list:
It's a function pointer.
It doesn't "do" anything, it's a type. It can be used to hold the address of a function.
When you need to store the address of a function.
Probably because you're on a 64-bit platform where pointers are 8 bytes.

Related

Are there differences between pointers to object stored on stack and heap? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Are there differences between pointers to object stored on stack and heap ? Are there internal representation in common C/C++ compilers (or JVM/LLVM) differs ?
this is very interesting question somehow related to main : memory location patterns on stack and heap
A pointer is a pointer. No matter where it points to.
I mean: you can assign to the same pointer both the address of a region on the stack and on the heap, don't you? So there cannot be any intrinsic difference between a pointer pointing here or there.
The difference being ones on the stack you do not need to free

Displaying pointer's address in C, without using printf? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to recode the printf function using only a handful of functions from the libc library, namely write, malloc and free and a few other basic ones. The only formatting tab that I am having trouble with is %p as I have no idea how to display it using the write function, as passing the address of my pointer to a function that prints strings resulted in empty output. I have also tried typecasting it to a char * and unsigned char * but that did not work either. I have looked online but only found solutions invloving forbidden functions so I am not even sure where to begin.

What's the difference between generic pointer and void pointer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What's the difference between generic pointer and void pointer?
void* gp;
No difference. void pointer is itself called generic pointer.
If you declare a pointer to void it's called Generic Pointer since you have to cast it to another kind of pointer first. Check the dictionary definition of generic. Both are the same, but this is just a 'description' of 'void pointer'.
This might seem useless but it has a very important use: you can use it multiple times to point at data of different types (int char etc..)

Why does C use the asterisk to reference the value of a pointer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.
I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.
I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.
Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.

How to access the data in a char array by having it's address in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am writing a system call function (in FreeBSD). As you may know the return type of such a function is an int which only indicates whether the syscall has been successful or not and the value which the caller has asked for is returned through setting:
td->td_retval[0]= my_requires_value;
where td is of type thread* and td_retval[0] is an int.
My problem is that my required value is a char []. How can I manage this? What should I set td->td_retval[0] as here in the syscall and how can I retrieve it back in the caller function?
I'm not very familiar with C and its pointers but I feel like this can be accomplished through using the right pointers and addresses.
If you want to return a string, your function should have an argument that is a pointer to a pre-allocated buffer in user space that will take the string (and another argument with the maximum size). The return value is only used to report errors, e.g. that the provided buffer is too small.
See this example, it may help you to design your syscall.

Resources