Thread: Copying data from another defined variable.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Copying data from another defined variable.

    Hey !

    if I have two int arrays with size 10 (assumable), lets say that's the name of first one is arr1 , the other one is arr2 . my question is when I write arr1=arr2, logically it means that copy the DATA of arr2 to the memory location of arr1, meaning with this, smashing all the data of arr1 and replacing it by the data of arr2. is that right? thanks !

    btw I know in standard types variables like integers, if I have int x ; int y; and write x=y , what actually happen copying the data of y to x. but I guess might be differently in arrays!

    Moreover, is it different(in aspect of memory and addressing) to write const int x=5; int y=x ; from const int x=5; int y=5 ((assuming the value of x is constant 5)) in an aspect of references ? because for instance in Jave if we write String x="Awesome"; String y=x ; it's referencing to the same value , but String x="Awesome"; String y="Awesome" , is totally not referencing to the same value .


    thanks !

    thanks alot!
    Last edited by RyanC; 12-06-2018 at 03:47 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    if I have two int arrays with size 10 (assumable), lets say that's the name of first one is arr1 , the other one is arr2 . my question is when I write arr1=arr2, logically it means that copy the DATA of arr2 to the memory location of arr1, meaning with this, smashing all the data of arr1 and replacing it by the data of arr2. is that right?
    No, it is absolutely wrong.

    Also, stop saying "smashing all the data"; that was from GReaper's bad analogy, right? The analogy just doesn't work: in C, if your variable is like a table that holds a value that is the cake, when you want to replace the cake, the cake is just magically cleanly replaced, with the previous cake disappearing into thin air. No smashing is involved. Obviously that doesn't make sense in real life, hence the analogy simply fails to convey the point.

    If you really want an analogy, use the analogy of an analogue clock, except that it has a single hand (i.e., the hour hand), and instead of 12 hours, it is numbered from say, 0 to 255 (the typical range of values for an unsigned char). When you start with the value of 5 and assign 10, what happens is that the hour hand starts pointing at 5, then upon the assignment, it very quickly changes to point at 10. You will find this analogy useful to understand modulo arithmetic, and indeed another name for that is "clock arithmetic".

    Quote Originally Posted by RyanC
    btw I know in standard types variables like integers, if I have int x ; int y; and write x=y , what actually happen copying the data of y to x. but I guess might be differently in arrays!
    Why guess? Write a program and find out why your assumptions do not hold:
    Code:
    #include <stdio.h>
    
    void print_array(int arr[], size_t size)
    {
        size_t i;
        for (i = 0; i < size; ++i)
        {
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    
    int main(void)
    {
        int arr1[10] = {0};
        int arr2[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        print_array(arr1, sizeof(arr1) / sizeof(arr1[0]));
        arr1 = arr2;
        print_array(arr1, sizeof(arr1) / sizeof(arr1[0]));
        return 0;
    }
    If you are right, the program will compile, and then you will see that before the assignment, arr1 consists of all 0s, and after the assignment, arr1 consists of the same values in the same order as arr2. Easy!

    EDIT:
    Quote Originally Posted by RyanC
    Moreover, is it different(in aspect of memory and addressing) to write const int x=5; int y=x ; from const int x=5; int y=5 ((assuming the value of x is constant 5)) in an aspect of references ? because for instance in Jave if we write String x="Awesome"; String y=x ; it's referencing to the same value , but String x="Awesome"; String y="Awesome" , is totally not referencing to the same value .
    C does not have Java-style reference objects. C does have a reference mechanism, but explicitly in the form of pointers. Java-style reference objects are like implicit C pointers without pointer syntax (and hence without pointer arithmetic).
    Last edited by laserlight; 12-06-2018 at 04:23 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    No, it is absolutely wrong.

    Also, stop saying "smashing all the data"; that was from GReaper's bad analogy, right? The analogy just doesn't work: in C, if your variable is like a table that holds a value that is the cake, when you want to replace the cake, the cake is just magically cleanly replaced, with the previous cake disappearing into thin air. No smashing is involved. Obviously that doesn't make sense in real life, hence the analogy simply fails to convey the point.

    If you really want an analogy, use the analogy of an analogue clock, except that it has a single hand (i.e., the hour hand), and instead of 12 hours, it is numbered from say, 0 to 255 (the typical range of values for an unsigned char). When you start with the value of 5 and assign 10, what happens is that the hour hand starts pointing at 5, then upon the assignment, it very quickly changes to point at 10. You will find this analogy useful to understand modulo arithmetic, and indeed another name for that is "clock arithmetic".
    If I tell you you're a superb instructor it wouldn't be sufficient ! thank you alot more than u guess !





    Why guess? Write a program and find out why your assumptions do not hold:
    Code:
    #include <stdio.h>
    
    void print_array(int arr[], size_t size)
    {
        size_t i;
        for (i = 0; i < size; ++i)
        {
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    
    int main(void)
    {
        int arr1[10] = {0};
        int arr2[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        print_array(arr1, sizeof(arr1) / sizeof(arr1[0]));
        arr1 = arr2;
        print_array(arr1, sizeof(arr1) / sizeof(arr1[0]));
        return 0;
    }
    If you are right, the program will compile, and then you will see that before the assignment, arr1 consists of all 0s, and after the assignment, arr1 consists of the same values in the same order as arr2.
    in a truth , I have applied like this program but didn't understand why it's working like this, meaning *I'm in doubt of what I'm going to say* when I write arr1=arr2, it means that the byte/house of "arr1" is pointing to arr2, so what have been hold on arr1 of consecutive data is just gone as trash in the memory because I don't have an access "pointer" to back to the data that arr1 was pointing !, i.e the array of data that arr1 was manager of them(pointing to this first byte of its array) is just stayed away in the memory and I don't have an access to it because of writing arr1=arr2;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    in a truth , I have applied like this program but didn't understand why it's working like this, meaning *I'm in doubt of what I'm going to say* when I write arr1=arr2, it means that the byte/house of "arr1" is pointing to arr2, so what have been hold on arr1 of consecutive data is just gone as trash in the memory because I don't have an access "pointer" to back to the data that arr1 was pointing !, i.e the array of data that arr1 was manager of them(pointing to this first byte of its array) is just stayed away in the memory and I don't have an access to it because of writing arr1=arr2;
    I bet you all the money in the world multiplied by whatever positive multiplier you desire that you did not compile and run my program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    I bet you all the money in the world multiplied by whatever positive multiplier you desire that you did not compile and run my program.
    I applied my program *not yours* now I applied and run it , it shows me in 18's code row : main.c:26:10: error: assignment to expression with array type

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I applied my program *not yours*
    Sure, but I have never seen your program. If you want to reason through a program with other people, you need to post its source code. I mean, you wrote 'when I write arr1=arr2, it means that the byte/house of "arr1" is pointing to arr2'. So... arr1 is a pointer? But what has that got to do with your question about arr1 being an array then?

    Quote Originally Posted by RyanC
    now I applied and run it , it shows me : main.c:26:10: error: assignment to expression with array type
    Exactly. You cannot assign to an array. Hence, your question about "when I write arr1=arr2, logically it means that (...)" doesn't make sense. It is like asking 'what does the English word "sdfkcvdhifvdyhi" mean?' Since you cannot assign to an array, it is meaningless to ask what does assignment to an array mean.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Exactly. You cannot assign to an array. Hence, your question about "when I write arr1=arr2, logically it means that (...)" doesn't make sense. It is like asking 'what does the English word "sdfkcvdhifvdyhi" mean?' Since you cannot assign to an array, it is meaningless to ask what does assignment to an array mean.
    hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhh killed me off and by the way can't assign because arr1 is a const array ! thanks alot!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-22-2016, 10:34 AM
  2. Replies: 16
    Last Post: 11-09-2011, 02:56 PM
  3. User defined Variable name
    By mat429 in forum C Programming
    Replies: 4
    Last Post: 08-28-2008, 08:07 AM
  4. Variable being used without being defined error
    By Moony in forum C Programming
    Replies: 2
    Last Post: 06-25-2006, 10:09 AM
  5. outputting a defined variable
    By rotis23 in forum C Programming
    Replies: 2
    Last Post: 11-12-2002, 05:00 AM

Tags for this Thread