Home > Back-end >  Value how to pass an array to a function?
Value how to pass an array to a function?

Time:01-24

The following procedures, both change1 change2 function, belong to the address, the value of the array elements are modified,
How to only the value of an array is passed? In order to modify the function elements, not to change the original array,

 
# include & lt; Stdio. H>

Void change1 (int * array, int n);
Void change2 (int array [], int n);

Int main () {
An int array []={1, 2, 3, 4, 5};
Change1 (array, 5);
for (int i=0; I & lt; 5; + + I) {
Printf (" % d ", array [I]);
}
}

Void change1 (int * array, int n) {
for (int i=0; I & lt; n; + + I) {
* (array + I)=0;
}
}

Void change2 (int array [], int n) {
for (int i=0; I & lt; n; + + I) {
Array [I]=0;
}
}

CodePudding user response:

Add a const array parameters should be able to achieve your requirement,

CodePudding user response:

Can put the whole array in structure, the parameters of the structure type,

CodePudding user response:

Limited is the structure of the array size must be constant,

CodePudding user response:

reference forever74 reply: 3/f
limited is the structure of the array size must be a constant,

Why so trouble, if it is a common variable is easy, preach value address a *,

CodePudding user response:

Because the designers hope C language on the one hand, simplify the complexity of the compiler, on the other hand to C ape to enough freedom,

If you really need to copy can completely in the superior (caller) functions in their processing, memcpy can be done,
  • Related