+ Post New Thread
Results 1 to 14 of 14
  1. #1
    Advanced Member level 5
    Points: 12,859, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,586
    Helped
    300 / 300
    Points
    12,859
    Level
    27

    Passing a pointer to an array as argument to function

    Hello,

    I have an array of floats declared as follows:
    Code:
    float some_array[3];
    I want to write a function that can receives a pointer to the above array:
    What's the correct syntax?

  2. #2
    Super Moderator
    Points: 250,799, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,661
    Helped
    13280 / 13280
    Points
    250,799
    Level
    100

    Re: Passing a pointer to an array as argument to function

    Code C - [expand]
    1
    
    func(float * arg)


    1 members found this post helpful.

  3. #3
    Advanced Member level 5
    Points: 12,859, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,586
    Helped
    300 / 300
    Points
    12,859
    Level
    27

    Re: Passing a pointer to an array as argument to function

    So,
    There's no need to let the function know that it's a pointer to an array of floats ?



    •   AltAdvertisment

        
       

  4. #4
    Super Moderator
    Points: 28,205, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,409
    Helped
    1064 / 1064
    Points
    28,205
    Level
    40
    Blog Entries
    6

    Re: Passing a pointer to an array as argument to function

    No, because inside the function you can declare a local variable with the same structure (eg. float another_array[3]), which will get the pointer of the external variable.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  5. #5
    Advanced Member level 5
    Points: 12,859, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,586
    Helped
    300 / 300
    Points
    12,859
    Level
    27

    Re: Passing a pointer to an array as argument to function

    So what will be the difference between these 2 examples:

    Code:
    func(float * arg)
    Code:
    func(float * arg[3])
    ?



    •   AltAdvertisment

        
       

  6. #6
    Advanced Member level 3
    Points: 6,527, Level: 19
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    836
    Helped
    343 / 343
    Points
    6,527
    Level
    19

    Re: Passing a pointer to an array as argument to function

    Quote Originally Posted by andre_teprom View Post
    No, because inside the function you can declare a local variable with the same structure (eg. float another_array[3]), which will get the pointer of the external variable.
    I don't understand why you want to declare a local array. It will not hold the same values as the array sent as the argument.
    If the argument is a pointer to float, it can be used directly as a float array:
    Code:
    #include < stdio.h > // Remove the spaces around stdio.h! If I remove them the line is shown completely wrong because of some strange bug in the forum.
    
    void func(float *arg){
      printf("arg[2] = %f\n", arg[2]);
    }
    
    void func2(float arg2[]){
      printf("arg2[2] = %f\n", arg2[2]);
    }
    
    int main(int argc, char *argv[]){
      float some_array[3];
    
      some_array[2] = 123.45;
      func(some_array);
      func2(some_array);
      return(0);
    }
    Result:
    Code:
    arg[2] = 123.449997
    arg2[2] = 123.449997
    Last edited by std_match; 15th November 2018 at 01:25.



    •   AltAdvertisment

        
       

  7. #7
    Super Moderator
    Points: 28,205, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,409
    Helped
    1064 / 1064
    Points
    28,205
    Level
    40
    Blog Entries
    6

    Re: Passing a pointer to an array as argument to function

    It was not mentioned by the OP if only some read-only operation would be done with the contents of the variable, and in that case according to your observation, it would not even be necessary to pass the variable to the function as an argument.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  8. #8
    Super Moderator
    Points: 250,799, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,661
    Helped
    13280 / 13280
    Points
    250,799
    Level
    100

    Re: Passing a pointer to an array as argument to function

    float * arg[3] is an array of float pointers, your C compiler will tell you if you try.

    float *arg and float arg[] are equivalent, as shown by std_match. You can use pointer and subscript access inside the function, no matter how the formal parameter is defined. The backside is that plain C doesn't provide range checking for pointer variables or arrays.


    1 members found this post helpful.

  9. #9
    Advanced Member level 5
    Points: 8,793, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,558
    Helped
    355 / 355
    Points
    8,793
    Level
    22

    Re: Passing a pointer to an array as argument to function

    Code:
    void out (void * value, char amount)
    {
        for (char i = 0; i < amount; i++)
            printf("Value %u = %.1f\n", i + 1, ((float*)value)[i]);
        printf("\n");
    }
    
    int main()
    {
        float _value = 3.14;
        
        out(&_value, 1); //parsing one value
        
        float _values[] = { 2.5, 3.1, 5.6 };
    
        out(_values, 3); //parsing array of values
        
        out(_values, sizeof(_values)/sizeof(float)); //flexible form
    
        return 0;
    }
    }
    $gcc -o main *.c
    $main
    Value 1 = 3.1

    Value 1 = 2.5
    Value 2 = 3.1
    Value 3 = 5.6

    Value 1 = 2.5
    Value 2 = 3.1
    Value 3 = 5.6
    Love me or hate me. All infractions will be ignored.


    1 members found this post helpful.

  10. #10
    Advanced Member level 5
    Points: 12,859, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,586
    Helped
    300 / 300
    Points
    12,859
    Level
    27

    Re: Passing a pointer to an array as argument to function

    float * arg[3] is an array of float pointers, your C compiler will tell you if you try.
    float *arg and float arg[] are equivalent, as shown by std_match.
    If I want to declare a pointer to an array of 3 elements in my code I use:
    Code:
    float (*pointer_to_array)[3]
    But you pointed out that if I want to pass a pointer to a function - I should write:
    Code:
    func(float * pointer_to_array)
    Why aren't both cases the same ?



  11. #11
    Advanced Member level 3
    Points: 6,527, Level: 19
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    836
    Helped
    343 / 343
    Points
    6,527
    Level
    19

    Re: Passing a pointer to an array as argument to function

    Quote Originally Posted by shaiko View Post
    If I want to declare a pointer to an array of 3 elements in my code I use:
    Code:
    float (*pointer_to_array)[3]
    But you pointed out that if I want to pass a pointer to a function - I should write:
    Code:
    func(float * pointer_to_array)
    Why aren't both cases the same ?
    The pointers don't have the same type. A pointer to a float array with 3 elements is not identical to a pointer to one float.
    Normally you want a pointer to an array to be a pointer to the first element ("float *pointer_to_array;").
    The reason is that you normally want an incremented pointer to point to the next element in the array.
    If you have a pointer type that points to a float array of size 3 and increment it, it will skip the entire array and point to the first location after the array.
    The compiler expects the pointer to be a pointer to an array of arrays, so when you increment it, it will point to the next array of size 3.

    Code:
    float (*pointer_to_array)[3];
    pointer_to_array = my_array;
    ponter_to_array++; // The pointer will not point to the second element in the array. It will point to the next float array of size 3
    Code:
    float *pointer_to_array;
    pointer_to_array = my_array;
    ponter_to_array++; // The pointer will point to the second element in the array.
    Code:
    #include < stdio.h > // Remove the spaces around stdio.h, it is a bug in the forum
    
    int main(int argc, char *argv[]){
      float (*pointer_to_array)[3];
      float *pointer_to_float;
    
      pointer_to_array = NULL;
      pointer_to_float = NULL;
      pointer_to_array++;
      pointer_to_float++;
      printf("pointer_to_array = %p\n", pointer_to_array);
      printf("pointer_to_float = %p\n", pointer_to_float);
      return(0);
    }
    Result:
    Code:
    pointer_to_array = 0xc
    pointer_to_float = 0x4


    1 members found this post helpful.

  12. #12
    Advanced Member level 5
    Points: 12,859, Level: 27
    Achievements:
    7 years registered

    Join Date
    Aug 2011
    Posts
    2,586
    Helped
    300 / 300
    Points
    12,859
    Level
    27

    Re: Passing a pointer to an array as argument to function

    And suppose I want to pass the function a pointer to a 3 element array (not only to the first one).
    What will be the syntax for that ?



    •   AltAdvertisment

        
       

  13. #13
    Advanced Member level 3
    Points: 6,527, Level: 19
    Achievements:
    7 years registered

    Join Date
    Jul 2010
    Location
    Sweden
    Posts
    836
    Helped
    343 / 343
    Points
    6,527
    Level
    19

    Re: Passing a pointer to an array as argument to function

    Code:
    #include < stdio.h > // A forum bug, remove the spaces around stdio.h
    
    float (*ptr_to_float_array_size_3)[3];
    float my_float_array[] = { 10.0, 11.0, 12.0 };
    
    void print_index_2(float *arg_array){
      // This is probably what you want
      printf("arg_array[2] = %f\n", arg_array[2]);
    }
    
    void stupid_function(float (*arg_ptr_to_float_array_size_3)[3]){
      // This is probably not what you want
      printf("Element at index 2 in the array = %f\n", (*arg_ptr_to_float_array_size_3)[2]);
      printf("Can also be accessed like this = %f\n", arg_ptr_to_float_array_size_3[0][2]); // Array with index zero, value at index 2 in that array
    }
    
    int main(int argc, char *argv[]){
      // An array name is a pointer to the first element
      print_index_2(my_float_array);
    
      // If you take the address of an array, you get a pointer to the whole array
      ptr_to_float_array_size_3 = &my_float_array;
      stupid_function(ptr_to_float_array_size_3);
    
      printf("An array name is a pointer to the first element, size = %lu\n", sizeof(*my_float_array));
      printf("The address of an array is a pointer to the whole array, size = %lu\n", sizeof(*(&my_float_array)));
      return(0);
    }
    Result:
    Code:
    arg_array[2] = 12.000000
    Element at index 2 in the array = 12.000000
    Can also be accessed like this = 12.000000
    An array name is a pointer to the first element, size = 4
    The address of an array is a pointer to the whole array, size = 12


    1 members found this post helpful.

  14. #14
    Advanced Member level 5
    Points: 8,793, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,558
    Helped
    355 / 355
    Points
    8,793
    Level
    22

    Re: Passing a pointer to an array as argument to function

    Quote Originally Posted by shaiko View Post
    And suppose I want to pass the function a pointer to a 3 element array (not only to the first one).
    What will be the syntax for that ?
    Code:
    void out (float value1, float value2, float value3)
    What I see here is that you can't understand the pointer itself. We don't pass arrays, we pass pointers.
    Love me or hate me. All infractions will be ignored.


    1 members found this post helpful.

--[[ ]]--