Like Tree2Likes
  • 1 Post By qny
  • 1 Post By christop

Keeping an Array the same after a function

This is a discussion on Keeping an Array the same after a function within the C Programming forums, part of the General Programming Boards category; Hi All, I would like to keep an array the same even after it is passed into a function. I ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    Keeping an Array the same after a function

    Hi All,

    I would like to keep an array the same even after it is passed into a function. I am doing different sorts on the same data but when i try using

    Code:
    num=temp
    HeapSort(temp,n);
    where n is the total number in the array and num is an array.
    When I try using this, num and temp is sorted completed. Not sure why. Is there anyway to keep num the same after a function is used on it. I am not using any pointers by the way.

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Ramkiller1 View Post
    Code:
    num=temp
    num is an array
    I very much doubt that!
    arrays cannot be used as lvalues: thay cannot be assigned to.
    I believe both num and temp are pointers (you might like to read the comp.lang.c. FAQ, especially section 6).

    The pointers point to the same memory. Whatever you do through one pointer is visible through the other.
    laserlight likes this.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by qny View Post
    I very much doubt that!
    arrays cannot be used as lvalues: thay cannot be assigned to.
    I believe both num and temp are pointers (you might like to read the comp.lang.c. FAQ, especially section 6).

    The pointers point to the same memory. Whatever you do through one pointer is visible through the other.
    Is there any solution to my issue then?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Make a copy of the array and pass the copy to the function.
    laserlight likes this.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by christop View Post
    Make a copy of the array and pass the copy to the function.
    thats why i used temp=num;

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by Ramkiller1 View Post
    thats why i used temp=num;
    But it did not work. It was used like a pointer

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    You have to make a separate copy in memory so that there are two arrays. A pointer to the original array will just make an alias for that array.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    You have to make a separate copy in memory so that there are two arrays. A pointer to the original array will just make an alias for that array.
    Any code come to mind?

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by Ramkiller1 View Post
    Any code come to mind?
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.
    Will Do

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by whiteflags View Post
    Try writing it yourself. First, allocate some memory, then copy the original array into the new array. Free the new array when you are done with it.
    Well i just did this
    Code:
    int * p;
    p=(int *)malloc(max*sizeof(int)); //Allocate size of array
    p=num;
    sort1(p,n);
    free(p);
    This did the same thing as using temp. Unless I copied it wrongly.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    You copied wrongly. What you did is set the pointer to point to the first element of the array, after having allocated memory that the pointer points to. Effectively, all you did was create a memory leak, and the free(p) results in undefined behaviour or a later double free error.

    Rather:
    Code:
    int *p = malloc(max * sizeof(*p));
    /* Loop over the elements of num and assign them to the elements of p */
    /* ... */
    sort1(p, n);
    free(p);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Thanks it worked, Guess I was thinking of it wrongly. Did I need to make a pointer then for this or just another array or are you showing me how to save space ?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    If you want to use dynamic memory allocation, then a pointer was the right approach. If not, another array of at least the same size would work. You cannot save space because you need to copy those elements.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Once you made a second array, I think that the function which you are after is memcpy().
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 02:31 AM
  2. Replies: 2
    Last Post: 02-21-2012, 09:14 PM
  3. Replies: 6
    Last Post: 12-07-2011, 06:23 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Keeping track of values without an array
    By m712 in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2002, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21