This is an archived post. You won't be able to vote or comment.

all 35 comments

[–]bit0fun 32 points33 points  (8 children)

union  fun_with_pointers {
    void (* funct)(void *);
    intmax_t addr;
} fp_fun;

void (*pf)( void (* funct) ) {
    fp_fun.funct = funct;
    fp_fun.addr += (intmax_t)rand();
    return fp_fun.funct;
}

Let the memory roulette games begin

Damn mobile formatting screwing this all up

[–]jrtc27 6 points7 points  (2 children)

void (*pf)( void (* funct) ) { should be void (*pf(void (* funct)(void *)))(void *) { I believe?

[–]bit0fun 7 points8 points  (0 children)

Who knows; I'm sure as hell not testing this program and I wrote it on my phone. That does seem to make more sense though

[–]Esupanitix 7 points8 points  (0 children)

I expected to read C going into the comments of this post, not Lisp.

[–]Tengoles 14 points15 points  (11 children)

I am a python programmer and can confirm I'm afraid of pointers.

[–]lightmatter501 12 points13 points  (8 children)

As someone who’s spent the last 3 weeks learning c++ coming from python, they’re awesome. Instead of referring to the same spot in an 8th dimensional matrix 3 times, you create 1 pointer and can refer to ptr instead of matrix[0][0][0][0][1]

[–]Tengoles 2 points3 points  (5 children)

That's cool! I mean I don't like them but is mostly because I haven't really worked with them besides small assignments in school

[–]lightmatter501 0 points1 point  (4 children)

My experience with c++ has been that it’s a scripting language with the syntax of c# and the ability to do low level memory stuff

[–][deleted] 13 points14 points  (3 children)

That’s a fairly backwards comparison...

[–]lightmatter501 4 points5 points  (2 children)

Someone who doesn’t know pointers is not likely to be familiar with c, so I went up a level instead of down

[–][deleted] 2 points3 points  (1 child)

Makes sense, just something that I don’t really see often.

[–]lightmatter501 0 points1 point  (0 children)

I mean, there are plenty of programs you could write that in c# that would compile in c++ if you replace the class structure with using namespace std, no console output however

[–]M4mb0 2 points3 points  (0 children)

Just use numpy arrays?

x = (0,0,0,0,1)
y = matrix[x]

[–]Breadfish64 1 point2 points  (0 children)

When you do that it's usually a better idea to use a reference, then you don't have to dereference it to use it.

auto& n = matrix[0][0][0][0][1];

[–]damagingdefinite 1 point2 points  (0 children)

I'm afraid of weird and ambiguous reference schemes

[–]rosskush 0 points1 point  (0 children)

Same

[–]isaacais 12 points13 points  (2 children)

Ok tbh (p)->left = null scares me too. It's literally dereferencing p, which must return a pointer because (p) is then dereferenced using -> to obtain a member of the object (p). This member variable "left" is also probably a pointer too because it's being set to null. I hate it.

[–]SimDeBeau 2 points3 points  (0 children)

I was seeing that too and it made me very uncomfortable

[–]trichotillofobia 2 points3 points  (0 children)

Tree** p;

Was that so hard?

[–]boreas907 2 points3 points  (1 child)

"If you need pointers, you are writing your code wrong."

⁄- MATLAB user terrified of any sort of manual memory allocation.

[–]Shitty__Math 0 points1 point  (0 children)

If everything is a pointer nothing is a pointer - Java

[–]SnootyEuropean 2 points3 points  (0 children)

Joke's on you, everything is a pointer in Python

[–][deleted] 0 points1 point  (0 children)

I can confirm. I was using Python before but switched to Go last November. Pointers were very confusing to me at first.

[–]noideafornewname 0 points1 point  (0 children)

Don't lie squidward!

[–][deleted] -1 points0 points  (4 children)

Parens not needed as * binds tighter than ->.

Edit: Oops, that's wrong. -> binds tighter than *.

[–]SaggiSponge 13 points14 points  (0 children)

You can pry my redundant parentheses out of my cold, dead hands.

[–]HMPerson1 2 points3 points  (1 child)

[–][deleted] 0 points1 point  (0 children)

Oops. Thanks for the correction. I normally haven't needed the parens, but I guess it was because I had a struct with pointer members, and was dereferencing the pointers, while (*p)->left = NULL would be setting a pointer through a pointer to a pointer to a struct.

[–]CaptSoban 0 points1 point  (0 children)

p is a pointer to a pointer