Author Topic: explanation of pointers in c  (Read 1739 times)

0 Members and 1 Guest are viewing this topic.

Offline -Polyphony-

  • Serf
  • *
  • Posts: 46
  • Cookies: 0
  • Python Programmer
    • View Profile
explanation of pointers in c
« on: 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...

Code: [Select]
#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
« Last Edit: September 09, 2012, 08:03:26 pm by skidiot.h »

Offline z3ro

  • Knight
  • **
  • Posts: 345
  • Cookies: 60
    • View Profile
Re: explanation of pointers in c
« Reply #1 on: September 09, 2012, 03:53:47 pm »
Nice share!  ;D  (even if I already knew all this  :P )
~ God is real. Unless declared as an integer.