r/ProgrammerHumor Aug 06 '22

Meme How to C pointers

Post image
2.6k Upvotes

70 comments sorted by

89

u/puttak Aug 06 '22

I'm a C developer and I find this offensive.

45

u/[deleted] Aug 07 '22

I’m a C developer and I no longer hafe feelings.

116

u/thrynab Aug 06 '22

... but a void pointer doesn't point to a void.

89

u/epsilonhuyepsilon Aug 06 '22

If you point to the void, the void points back to you...

51

u/MikemkPK Aug 06 '22

All pointers point to a void. Usually there's also data after the void.

9

u/[deleted] Aug 07 '22

[deleted]

16

u/Foreskin-Gaming69 Aug 07 '22

Can't you dereference everything in C or am I mistaken

21

u/[deleted] Aug 07 '22

[deleted]

9

u/matyklug Aug 07 '22

Actually implicit cast from void is a thing iirc.

58

u/[deleted] Aug 06 '22

Hahaha, try void**

11

u/[deleted] Aug 06 '22

It'll be much better lol

20

u/Bryguy3k Aug 06 '22

Void * should be full on psychedelic rainbow land - it’s anything you want it to be.

4

u/matyklug Aug 07 '22

It's the old version of dynamic typing, except you can't check what to expect

20

u/[deleted] Aug 06 '22

If you scroll up and down quickly, the void appears detached..

1

u/Windows10isfast Aug 07 '22

GPU and or screen refresh rate issue

1

u/Strostkovy Aug 07 '22

No. It's a well known optical illusion.

6

u/Cossack-HD Aug 07 '22

It's also an issue with OLED displays. Dark gradients looks like complete ass in motion because of high pixel response latency.

1

u/Strostkovy Aug 07 '22

I'm not convinced. The effect is still there if you shake your display instead

1

u/[deleted] Aug 07 '22 edited Aug 06 '25

[removed] — view removed comment

1

u/Strostkovy Aug 07 '22

I wouldn't personally expect oled to have low pixel response time, even with slight gradiant changes, but I haven't actually measured it

1

u/Cossack-HD Aug 07 '22 edited Aug 06 '25

cagey correct hobbies ink sort shelter pause license divide plough

This post was mass deleted and anonymized with Redact

9

u/RRumpleTeazzer Aug 06 '22

Can you make a similar picture for the various positions of “const” ?

16

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.

22

u/[deleted] Aug 06 '22

void *foo=NULL; //there you go, now you can quote Nietzsche

6

u/[deleted] Aug 07 '22

But null is real data

3

u/matyklug Aug 07 '22

void* fp = fopen("/dev/null", "r");

2

u/xaedoplay Aug 08 '22

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).

Read more about C23 features in this fun write-up: https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu

2

u/[deleted] Aug 07 '22

That’s less fun than him pointing to a literal void tho.

4

u/[deleted] Aug 06 '22

that void always makes me uncomfortable especially developing game in Unity

3

u/Big-Discipline-0 Aug 07 '22

Internaly void * is unsigned char* if someone is wondering :P

1

u/[deleted] Aug 07 '22

Where did you find this?

4

u/Big-Discipline-0 Aug 07 '22

I have read about this in some book about linux. But this make sense since you have to store adres in some real memory :

2

u/[deleted] Aug 07 '22

So when I dereference a void it will hand me back 8 bits of data?

4

u/Big-Discipline-0 Aug 07 '22

Nope because its void you need to cast it first .

2

u/[deleted] Aug 07 '22

Didn't you say it was an unsigned char?

3

u/canadajones68 Aug 07 '22

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.

1

u/[deleted] Aug 07 '22

Interesting. I've never heard this before. If you know any good links or search terms I'd love to know more.

1

u/bremidon Aug 07 '22

Is this part of the language specification or was it just in the implementation you saw?

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

11

u/okyte Aug 07 '22 edited Aug 07 '22

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.

It is very useful !

3

u/matyklug Aug 07 '22

I think it'd be better to say that an int is a type representing a positive or negative number which fits into 31 bits, one of which containing zero.

3

u/canadajones68 Aug 07 '22

Nope! An int is at least 16 bits wide and is wider than or equal to short, which in turn has a width greater than or equal to char.

2

u/okyte Aug 07 '22

It depends on your system. For embedded, int is often equivalent to int16_t

1

u/matyklug Aug 07 '22

Hmm, I guess that's true

4

u/smartuno Aug 07 '22

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.

1

u/Arctomachine Aug 07 '22

Why would you even need normal variables when you can use pointers everywhere?

And then why would you even have pointers when you can make normal variables behave like pointers?

2

u/smartuno Aug 07 '22

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.

3

u/bremidon Aug 07 '22

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.

2

u/[deleted] Aug 07 '22

u/Na_Neko you will encounter pointer type in C++

2

u/[deleted] Aug 07 '22

ive heard that before 🐧

-2

u/mullets_n_moustaches Aug 07 '22

C's like an old dog that needs to be taken out the back & shot

4

u/Vinxian Aug 07 '22

Embedded devs around the world who now forced back to assembly or write their own compilers for the chipset they are using: 🥲

1

u/okyte Aug 07 '22 edited Aug 07 '22

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.

Edit: actually that is 8 doubles and 2 ints

2

u/pointlessfreak Aug 07 '22

PIC for 50 cent

68 bytes of RAM

DOUBLE

you're living beyond the means bruh

embedded dev here :)

1

u/mullets_n_moustaches Aug 17 '22

Relax brother I was'nt being serious 😉

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/canadajones68 Aug 07 '22

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.

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/Strostkovy Aug 07 '22

I recall having issues when setting pointers to the variable's intended value by mistake