Thread: Arrays!

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    Arrays!

    Hello,
    I want to pass an array to a function and change the array, how can I do that? Here's my code:
    Code:
    #include<stdio.h>
    
    /*function to change the array*/
    void rearng(int *qw)
    {
         int i,j,a;
         int sa[8];  //temp array
         int c = 0;
         int high;
         
         /*find highest number in the array*/
         high = qw[0];
         int u;
         for(u = 1; u < 8; u++)
         {
               if(qw[u] > high)
                  high = qw[u];
         }
        
         /*arrange the array from high to low*/
         for(i = 0; i <= high; i++)
         {
               for(j = 0; j < 8; j++)
               {
                     if(i == qw[j])
                     {
                          sa[c] = qw[j];
                          c++;
                     }
               }
         }
        /*make the temp array, real array*/
         qw = sa;
        
    }            
    
    int main(void)
    {
        int qw[] = {12,43,3,234,21,35,213,12}; //real array
        
        rearng(qw); // call the function
        
        /*problem: s not changing the real array*/
        int i;
        for(i = 0; i < 8; i++)
           printf("%d ",qw[i]);
           
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The temp array is just that - an automatic variable who automatically goes out of scope (goes to byte heaven), when the function ends.

    You could malloc the temp array to make this work, or less efficiently, copy the temp array back to the permanent one. Either way. qw is a constant pointer and can't be changed. You are not receiving a warning about this?
    Last edited by Adak; 10-01-2013 at 03:00 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can't reassign arrays, directly or indirectly.

    Also assignments to variables inside function are local to that function.

    To understand what is happening though you need to understand the meaning of passing by value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    with your code, you also risk buffer overruns. what happens if you send an array smaller than 8 elements to rearng()? how do you tell rearng() that the incoming array is less than 8 elements?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by Elkvis View Post
    with your code, you also risk buffer overruns. what happens if you send an array smaller than 8 elements to rearng()? how do you tell rearng() that the incoming array is less than 8 elements?
    I guess by; sizeof(array)/sizeof(int) ; is this correct?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Harith View Post
    I guess by; sizeof(array)/sizeof(int) ; is this correct?
    Depends on where you planning to do it...

    And I prefer type independent version

    Code:
    sizeof(array)/sizeof(array[0]);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Harith View Post
    I guess by; sizeof(array)/sizeof(int) ; is this correct?
    Not necessarily.

    When passing an array as an argument to a function, the function receives a pointer, not an array.

    So
    Code:
    void fun(int *array)
    {
          /* sizeof(array) will be the size of a pointer here .... 4 on a typical 32-bit system */
    }
    
    int main()
    {
         int array[20];
         /* sizeof(array) will be 20 times sizeof(int) here */
    
        fun(array);     /*  Note comment above for what sizeof(array) will produce in fun() */ 
    }
    The "type independent version" mentioned by vart is better practice .... but still won't produce the desired result in fun().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

Tags for this Thread