Like Tree4Likes
  • 4 Post By vart

Arrays!

This is a discussion on Arrays! within the C Programming forums, part of the General Programming Boards category; Hello, I want to pass an array to a function and change the array, how can I do that? Here's ...

  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,440
    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%.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    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?
    Code:
    namespace life
    {
        const bool change = true;
    }

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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]);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,440
    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%.

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, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05: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


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