-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from h1110050/add-pointer-functions
An example of how to use pointers in functions in C
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* This program swaps values by passing variables as pointers into a function in C. | ||
* Author: h1110050 | ||
* Profile: http://github.com/h1110050 | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
void swap(int *, int *); | ||
|
||
int main(void) { | ||
int a = 1, b = 3; | ||
|
||
printf("a is %d, b is %d\n", a, b); | ||
|
||
// Pass in the address if you want the change to persist in main function. | ||
swap(&a, &b); | ||
|
||
printf("a is %d, b is %d\n", a, b); | ||
|
||
return 0; | ||
} | ||
|
||
void swap(int *a, int *b) { | ||
int temp; | ||
|
||
// Swap the pointers. | ||
temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} |