Skip to content

Commit

Permalink
Merge pull request #16 from h1110050/add-pointer-functions
Browse files Browse the repository at this point in the history
An example of how to use pointers in functions in C
  • Loading branch information
nayyyhaa authored Oct 27, 2019
2 parents 102a6c3 + 68765e0 commit 0f0749f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pointer_functions.c
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;
}

0 comments on commit 0f0749f

Please sign in to comment.