Like Tree1Likes
  • 1 Post By Click_here

How do you pass an array into a function??

This is a discussion on How do you pass an array into a function?? within the C Programming forums, part of the General Programming Boards category; Hi everyone. I am having a simple, noob problem with parameter passing. Say I have Code: int j[]={4, 2, 3, ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    How do you pass an array into a function??

    Hi everyone. I am having a simple, noob problem with parameter passing.

    Say I have
    Code:
    int j[]={4, 2, 3, 5,6}
    I try to pass something like this to my function
    Code:
    void randomSort(int arr[]){
            .......
    }
    int main(){
    randomSort(j);
    }
    When I go into the function with the array j and check the size, it comes out to size 1 as if only passing the first value!

    I am checking the size value with
    Code:
    int arraysize = sizeof(array)/sizeof(*array);
    Is there something wrong with my parameter type? or is it something else? Thanks!

    EDIT: found that my way of computing arraysize = sizeof(array)/sizeof(*array) is wrong, continuing to work on the sort (which I found after looking on the internet).
    Last edited by workisnotfun; 11-18-2012 at 08:24 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I found that my way of computing arraysize = sizeof(array)/sizeof(*array) is wrong and I can only access the first element of the array for some reason. I tried
    Code:
    void randomSort(unsigned long array[]){
      unsigned long *pointer;
      for(u=0,pointer = array;u<5;pointer++,u++){
        printf("in %i/%i, ",u,*pointer);
        size++;
        u++;
      }
    }
    and this gets all the elements but continues until a seg fault.
    How am I suppose to do this??
    Last edited by workisnotfun; 11-18-2012 at 08:41 PM.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    You can't use sizeof like that, only the function which it is defined

    The easiest way of solving your problem is by making a second input parameter which passes the result from sizeof from the calling function

    [edit]
    Code:
    void randomSort(unsigned long array[], size_t arr_size);
    
    /* and used like this */
      randomSort(array, sizeof(array)/sizeof(*array));
    [/edit]
    Last edited by Click_here; 11-18-2012 at 09:25 PM.
    rags_to_riches likes this.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    You can pass the array and it's size to the function.

    Code:
    void randomSort(int arr[], int size)
    .
    .
    int main(void)
    {
    int j[]={4, 2, 3, 5,6}
    .
    .
    randomSort(j, 5);
    .
    .
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I changed it to this
    Code:
    void sort(unsigned long array[],int size){
      int u;
      printf("sizeof the array = %i\n",size);
      //  unsigned long pointer[]=*array;
      for(u=0;u<size;u++){
        printf("in %i/%i, ",u,array[u]);
      }
    but I get a seg fault before the loop even begins. What could be wrong?
    Last edited by workisnotfun; 11-18-2012 at 09:19 PM.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    I changed the parameters to (unsigned long (*array)[], int size) and its going well for now

    How can I move the contents of one array to another? I want to do something like this
    Code:
      unsigned long move[size];
    ..//move values of unsorted (*array) and into sorted move
    *array=move; //make the original array point to the sorted one
    but I keep getting errors with *array=move, any ideas?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    sizeof returns the amount of bytes used, not the amount of elements in the array.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Can I create pointer variables to point to move and (*array)? But then how would I use them as if they were arrays?

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Is this what you are after?

    Code:
    void print_float_array(float (*an_array)[], size_t arr_size);
    
    
    int main(void)
    {
        float an_array[] = {1, 2, 3, 4};
    
    
        print_float_array(&an_array, sizeof(an_array)/sizeof(*an_array));
    
    
        return EXIT_SUCCESS;
    }
    
    
    void print_float_array(float (*an_array)[], size_t arr_size)
    {
        int i;
    
    
        for (i=0; i<arr_size; i++)
        {
            printf("%f ",(*an_array)[i]);
        }
    
    
    }
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Yea. I also already had that. At my current point I am sorting the array and putting the values in another array ( move ). then I want to do something like (*array) = move, but I am getting errors. I have to do it this way because I am using radix sort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 08:44 AM
  2. Can't pass array to function
    By yougene in forum C Programming
    Replies: 9
    Last Post: 08-19-2007, 05:09 PM
  3. how can i pass this array to my function?
    By kosodo in forum C Programming
    Replies: 4
    Last Post: 04-14-2006, 10:40 PM
  4. Replies: 3
    Last Post: 04-02-2002, 01:39 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

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