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..)
Related
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 9 months ago.
Improve this question
I'm still very new to C, have only been learning for about a week at this point, but something I don't get is the use of pointers. I get how pointers work and I know how to use them, but I don't see why or when they should be used. Can someone please explain this to me?
It is enough to mention that arrays used in expressions with very rare exceptions are implicitly converted to pointers to their first elements.
For example if you will write the expression
arr[i]
where arr is the array name then even in this expression the array is converted implicitly to a pointer to its first element.
Or another simple example: all C string functions deal with pointers of the type char * because passed arrays again are implicitly converted to pointers to their first elements.
Think about how to write a general sort function for arrays of any types.
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 2 years ago.
Improve this question
This is my code. When i run the code it sends me an error:
helloworld.c:8:16: error: storage size of 'bin' isn't constant
static int bin[size];
but when i do a build the exe works. Help pls.
The initializer of a static variable must be known at compilation time. That's why the declaration of bin is rejected. Instead you need to declare bin as a pointer to an integer and allocate memory dynamically, that is at run-time:
int *bin = malloc(size * sizeof *bin);
As pointed out by rioV8, you should also free the allocated memory with free(bin) when you are done with bin.
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.
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.
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 8 years ago.
Improve this question
This is a slightly subjective answer, but how large do you think a structure should be before you start using pointers to it in other structures or function calls rather than the structure by-value?
Depends on the compiler and architecture.
C and C++ define the size of the types they use, and how functions are written, but they don't define how they are implemented.
This is means the standard itself doesn't define how the structurs are passed, just that they are in essence copied.
The compiler might decide to do something else entirely, like not copying the struct at all if there's a default copy constructor , and the variable isn't used.
But after saying all that, most common-sense compiler implementations would store the struct in a register if it fits. So depends on the architecture, make sure the structure fits in a register.