Skip to content

Instantly share code, notes, and snippets.

@hamidreza-s
Last active January 1, 2016 00:48
Show Gist options
  • Save hamidreza-s/8068457 to your computer and use it in GitHub Desktop.
Save hamidreza-s/8068457 to your computer and use it in GitHub Desktop.
An example to how pointers in C work.
int main()
{
int i, j;
int *p;
p = &i;
*p = 5;
j = i;
printf("addresses: i = %d, j = %d, p = %d\n", &i, &j, p);
printf("values: i = %d, j = %d, p = %d\n", i, j, *p);
}
#include <stdio.h>
void swap1(int *i, int *j)
{
int t;
t = *i;
*i = *j;
*j = t;
}
void swap2(int *i, int *j)
{
int *t;
t = i;
i = j;
j = t;
}
void main()
{
int a, b;
a = 5;
b = 10;
printf("%d %d\n", a, b);
swap1(&a, &b);
printf("%d %d\n", a, b);
swap2(&a, &b);
printf("%d %d\n", a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment