0

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]);
        }
6
  • 3
    Please post a Minimal, Reproducible Example. Are you sure you allocated 2 arrays instead of doing like int statesVactor[n]; int* denominatorFactors = statesVactor;? Commented Dec 6, 2020 at 8:39
  • I added more code. I hope it's better. I am really not sure what I am doing. I've always used Java, it's my first project in C and everythig is...different. I think I did what you suggested I shouldn't. Commented Dec 6, 2020 at 9:01
  • Allocating zero-element array like 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) Commented Dec 6, 2020 at 9:04
  • You're right, my bad. Forgot to add scanf("%d", &n); Commented Dec 6, 2020 at 9:11
  • Reading of n must be before the declarations of arrays. Also the elements of statesVector should be initialized before the loop, of undefined behavior will be invoked by using values of uninitialized non-static local variables, which are indeterminate. Commented Dec 6, 2020 at 9:12

1 Answer 1

1

You can't initialize the size of array at 0 (int n = 0; int denominatorFactors[n])

You declarate your array after you read the size not before

#include<stdio.h>

int main(void)
{
    int size;
    do
    {
         printf("Give the size of your array :");
         scanf("%d",&size);
    }while(size <1);
    int statesVector[size];
    int denominatorFactors[size];
    for(int i = 0; i<size;i++)
    {
        printf("Give me the %d elment :",i);
        scanf("%d",&statesVector[i]);
        denominatorFactors[i] = statesVector[i];
    }
    printf("\n\nDisplay of the original array :\n\n");
    for(int i = 0; i<size;i++)
    {
        printf("[%d]\t",statesVector[i]);
    }
    printf("\n\nDisplay of the copy of array :\n\n");
    for(int i = 0; i<size;i++)
    {
        denominatorFactors[i]*=-1;
        printf("[%d]\t",denominatorFactors[i]);
    }
    printf("\n");
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.