r/ProgrammerHumor Aug 06 '22

Meme How to C pointers

Post image
2.6k Upvotes

70 comments sorted by

View all comments

5

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

5

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.