Skip to content

Instantly share code, notes, and snippets.

@tejainece
Created May 3, 2014 19:36
Show Gist options
  • Select an option

  • Save tejainece/85e0f03340e4ea7a8172 to your computer and use it in GitHub Desktop.

Select an option

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
#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);
}
#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