Last active
January 1, 2016 00:48
-
-
Save hamidreza-s/8068457 to your computer and use it in GitHub Desktop.
An example to how pointers in C work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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