Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Passing a pointer to an array as argument to function

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
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?
 

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

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.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
So what will be the difference between these 2 examples:

Code:
func(float * arg)

Code:
func(float * arg[3])

?
 

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:

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.
 

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.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
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
 
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 ?
 

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
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
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:
#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
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
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.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top