Created
May 3, 2014 19:36
-
-
Save tejainece/85e0f03340e4ea7a8172 to your computer and use it in GitHub Desktop.
In C, Arrays are always passed as reference while scalar Variables and Structures are passed as value
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> | |
| #include <string.h> | |
| void printArray(int a[]) { | |
| int i = 0; | |
| for(i = 0; i < 10; i++) { | |
| printf("%d ", a[i]); | |
| } | |
| printf("\n"); | |
| } | |
| void changesArray(int a[10]) { | |
| int i = 0; | |
| for(i = 0; i < 10; i++) { | |
| a[i] += 5; | |
| } | |
| } | |
| main() | |
| { | |
| int a[10] = {0}; | |
| int i = 0; | |
| for(i = 0; i < 10; i++) { | |
| a[i] = i; | |
| } | |
| printArray(a); | |
| changesArray(a); | |
| printArray(a); | |
| } |
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> | |
| #include <string.h> | |
| typedef struct Struct { | |
| int a; | |
| } Struct; | |
| void printStruct(Struct st) { | |
| printf("%d\n", st.a); | |
| } | |
| void changesStruct(Struct st) { | |
| st.a++; | |
| printStruct(st); | |
| } | |
| main() | |
| { | |
| Struct st = {5}; | |
| printStruct(st); | |
| changesStruct(st); | |
| printStruct(st); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment