EvilZone

Programming and Scripting => C - C++ => : -Polyphony- September 08, 2012, 05:24:12 PM

: explanation of pointers in c
: -Polyphony- September 08, 2012, 05:24:12 PM
to me, the syntax of pointers was never really explained to me thoroughly, but I've got some code that I think will help anyone learn the difference of the * operator and & and whatnot...

:
#include <stdio.h>

void task(x, y);

int main()
{
    int x;
    int y;
    x = 10;
    y = 100;
    printf("x = %d\n", x);
    printf("the address of x: %p\n", &x);
    printf("y = %d\n", y);
    printf("the address of y: %p\n", &y);
    task(&x, &y);
    return 0;
}

void task(int *p1,int *p2)
{
    printf("in task() x = %d\n", *p1);
    printf("and in task() y = %d\n", *p2);
    printf("also, in task()... the address of y = %p\n", p2);
    printf("and in task(), the address of x = %p\n", p1);
}

this kind of program is really what it took for me to really understand pointers and their significance. I hope it will help someone else :)


This youtube video also explains pointers alot

http://www.youtube.com/watch?v=Rxvv9krECNw#

Staff Edit
Do not double post, simply edit your post
: Re: explanation of pointers in c
: z3ro September 09, 2012, 03:53:47 PM
Nice share!  ;D  (even if I already knew all this  :P )