I need to copy an array to another, then change copied array without affecting the original one. I tried:
memcpy(statesVactor,denominatorFactors,n *sizeof(int));
and
memmove(statesVector,denominatorFactors,n *sizeof(int));
But still when I multiply statesVector by -1, denominatorFactors multiplies as well.
I feel, I don't understand sth simple.
EDIT:
int n = 0;
int denominatorFactors[n];
int statesVector[n];
scanf("%d", &n);
memmove(statesVector,denominatorFactors,n *sizeof(int)); //or
//memcpy(statesVactor,denominatorFactors,n *sizeof(int));
for(int i = 0; i<n;i++){
statesVector[i] = -1 * statesVector[i];
}
for(int i = 0; i<n;i++){
printf("%d\t",statesVector[i]);
}
for(int i = 0; i<n;i++) {
printf("%d",denominatorFactors[i]);
}
int statesVactor[n]; int* denominatorFactors = statesVactor;?int n = 0; int denominatorFactors[n];is not allowed. The number of elements of array must be greater than zero. (reference: paragraph 5 of N1570 6.7.6.2 Array declarators)nmust be before the declarations of arrays. Also the elements ofstatesVectorshould be initialized before the loop, of undefined behavior will be invoked by using values of uninitialized non-static local variables, which are indeterminate.