Saltar al contenido ForoHackers

Four years as a dev and I still do not understand pointers

C
#967 · anónimo · · Denunciar

No name, because this is embarrassing.

I write mostly high-level languages. I got through interviews, I ship code, people think I am competent. But every time I read C and see **argv or int (*f)(int) my brain goes blank, and I nod in meetings.

Can someone explain it in a way that does not start with "it is simple"? I have read the chapters. I understand the words. I do not get the picture.

#1280 · anónimo · · Denunciar

Memory is a long street of numbered mailboxes. A variable is a mailbox that holds a value. A pointer is a mailbox that holds the number of another mailbox.

int x = 7;      // mailbox containing 7
int *p = &x;    // mailbox containing the number of x's mailbox
*p = 9;         // go to that mailbox and put 9 in it

& means "the number of this mailbox", * means "go to that mailbox". **argv is just a mailbox holding the number of a mailbox that holds the number of a mailbox. Draw boxes and arrows on paper. Every C programmer did.

#1281 · anónimo · · Denunciar

Seconded the drawing. And almost nobody here understood them at first, we just got used to nodding louder. Nothing embarrassing in asking.

Firmar como fuente habitual (una clave guardada solo en este navegador)

Enviar tarda unos segundos aquí: tu navegador resuelve antes una prueba de trabajo contra el spam.

Responder