Reputation: 195
I want to pass a array to function and I want to change it within the function without changing the original array using c. Can I do it?
This is what I have so far:
#include <stdio.h>
void display(int age[]) {
age[0]=3;
age[1]=4;
}
int main() {
int ageArray[] = { 2,3 };
display(ageArray);//Passing array element ageArray[2] only.
printf("%d", ageArray[0]);
return 0;
}
Upvotes: 0
Views: 201
Reputation: 13690
C doesn't have to concept of passing arrays to a function. There is always a pointer passed to the function independent of the function signature:
void function(int *Array);
void function(int Array[]);
You must make a copy of the array if you want to avoid modification of the original array. For that purpose you must pass the size of the Array:
void function(int *Array, size_t Size);
Now you can create the copy.
Upvotes: 3