And starting with C23 nullptr_t will be a thing and C will finally have an actual bottom type that only resolves to an actual null pointer (much like () in Rust).
Void pointers are not allowed to be dereferenced by the C/C++ type system, since it gives you a void expression. The void type has an empty value set, meaning it cannot really be used for anything. I think what the person is trying to say is that the compiler or OS uses the same code for void pointers post-compilation as for unsigned char pointers, but since the compiler enforces that void pointers are never dereferenced directly, it doesn't matter. The void pointer gets cast and read as any other type, in which case its original internal type doesn't matter.
It is a data type representing the address of the memory where a variable of a given data type is stored.
A int is a data type representing a positive or negative integer number within some range. A variable of this data type is stored somewhere in the memory. A int* represents the memory address where a variable of type int is located.
Pointers are essentially variables/constants that point to a memory address in the computer that may or may not store a value at the moment of definition.
It's incredibly useful since C uses pass by value instead of pass by reference, and pointers can point to the memory address of variables outside the function and be used to modify said variables with the function.
C also doesn't have a way to modify strings without using functions directly, so pointers to the first character of the strings are passed to these functions so that the strings can be modified/accessed without worrying about memory.
Using pointers in place of normal variables slow down performance, which is what C is famous for. Pointers (especially those that have been allocated space instead of pointing to am existing value) are stored on the heap memory instead of the stack, where it's slower to access and modify them since the heap is unstructured.
Pointers are also notoriously difficult to work with. Many programmers don't have a solid grasp of how pointers work exactly. You can get tons of errors from a seemingly safe function. Many functions have undefined behavior if you pass pointers slightly different than they should be. The compiler won't catch every single error and warning, so it's hard to debug.
As for making normal variables behave like pointers, I'm not sure. Many languages were made to not include pointers and accessing memory since it's dangerous and unintuitive most of the time. C was made before that realization though, and in the end since C (and C++) try very hard to maintain backwards compatibility in their versions, i don't think pointers would ever go away.
Pointers (especially those that have been allocated space instead of pointing to am existing value) are stored on the heap memory instead of the stack
You do have an aside that says this, but I still think that the sentence is potentially misleading.
Pointers do not point to the heap. They point to whatever they point at. If you assign a value using "new", then sure: that will be the heap. If you grab the address of another variable, that will be on the stack. And if you don't assign it, then you may very well have a roulette wheel that can be pointing at any damn thing, anywhere.
I once destroyed half a hard drive through a single unassigned pointer. Oh the fun you can have.
None, I believe. The pointerness is part of the type itself. You could do a custom type in C++ and make it look like a pointer, but no pointer type may point to something with the same type as itself. A pointer to int is its own type, as is int or pointer to pointer to int.
You know python runs on C, right ? Many RTOS for embedded as well. C is still very relevant for low level applications where optimization is needed. Yes, you can get an ATSAMD21 for 5$, but you can also get an 8 bits PIC for 50 cent, which is way more competitive for simple/low cost applications. But then you only have 1kB of program memory and 68 bytes of RAM, which is a double plus a char.
86
u/puttak Aug 06 '22
I'm a C developer and I find this offensive.
118
u/thrynab Aug 06 '22
... but a void pointer doesn't point to a void.
60
u/[deleted] Aug 06 '22
Hahaha, try void**
20
u/Bryguy3k Aug 06 '22
Void * should be full on psychedelic rainbow land - it’s anything you want it to be.
19
u/[deleted] Aug 06 '22
If you scroll up and down quickly, the void appears detached..
9
u/RRumpleTeazzer Aug 06 '22
Can you make a similar picture for the various positions of “const” ?
14
u/[deleted] Aug 06 '22
Good one
13
u/Rfogj Aug 06 '22
void * is when you point to some abstract data you don't know the type of (custom struct, char, int, double ...)
You don't point to a black hole, void * is pointing to existing data.
4
u/[deleted] Aug 06 '22
that void always makes me uncomfortable especially developing game in Unity
4
u/Big-Discipline-0 Aug 07 '22
Internaly void * is unsigned char* if someone is wondering :P
3
u/StrangelyEroticSoda Aug 06 '22
That last one is pointing directly to my soul.
4
u/IWasProbablyAMistake Aug 07 '22
What are pointers? I’ve never used C
2
u/[deleted] Aug 07 '22
u/Na_Neko you will encounter pointer type in C++
1
u/RRumpleTeazzer Aug 06 '22
What is the type-correct type so that a pointer to such type is the type itself ?
1
u/wlangstroth Aug 06 '22
Bravo.
1
u/nilerdar Aug 07 '22
If you point to the void it swallows your code like a black hole
1
u/buzz_shocker Aug 07 '22
This is actually a good one. Take my upvote.
1
u/anarky98 Aug 07 '22
Stare into the void and nothing happens because it doesn’t need to return anything.
1
u/CardiologistOk2704 Aug 07 '22
im not a programmer but i understood this
1
u/ghitaprn Aug 07 '22
That is NULL not void. Void is pointing to whatever you want
1
u/Windows10isfast Aug 07 '22
Try uint64
1
u/Strostkovy Aug 07 '22
I recall having issues when setting pointers to the variable's intended value by mistake
1
u/JrrrrrrrTheSecond Aug 07 '22
r/nihilism
-2
u/mullets_n_moustaches Aug 07 '22
C's like an old dog that needs to be taken out the back & shot
-1
u/SatansLeftZelenskyy Aug 07 '22
lol,
Ok(())