About functions, Pointers and arrays

how can i write a function that takes as arguments:
(1) a pointer to an array of floats and
(2) the array length N and reverses the order of the array elements.
(1) void foo(float (*arr)[]);
(2) void bar(float/*?*/ arr[N]); //←N should be compile-time constant
closed account (G309216C)
Hi,

If this is your Home Work or project or whatever no one is just going to give you code. After all show us what you did till now. Then people can help you.

Thanks
Last edited on
By swapping first elements of the array with its last elements.
It's just for understanding the passing of pointers and arrays to a function,
and then finding the way of invocation.
Passing arguments to functions:
(1)
1
2
float x[10];
foo(&x);

(2)
1
2
3
//const int N(10); Somewhere before bar() declaration
float y[N];
bar(y);
If this is your Home Work or project or whatever no one is just going to give you code. After all show us what you did till now. Then people can help you.


If you have knowledge of the answer being sought, there is no rule that says you can't share your ideas. If you have no answer, there is no need to post; it's just spam.
any suggestion to solve would be appreciated...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void function(float (*arr)[])
{
	float temp[N];
	for (int i = N, j=0; i >= 0; i--, j++)
	{
		temp[0] = (*arr)[i];
	}

	(*arr)[N] = temp[N];
}


void main()
{
	cout<<"hello"<<endl;
	
	float arr[N];
	arr[0] = 10;
	arr[1] = 20;
	arr[2] = 30;
	arr[3] = 40;
	arr[4] = 50;

	function(&arr);// ERROR

	for (int n= 0; n < N; n++)
	{
		cout<<arr[n]<<endl;
	}


	char ch;
	cin>> ch;
}
It is interesting do you read sometimes what you are writing yourself? In your first post there is written enough clear that

how can i write a function that takes as arguments:
(1) a pointer to an array of floats and
(2) the array length N and reverses the order of the array elements.


Do you see number 2 in parentheses?

Now count number of arguments in your function

void function(float (*arr)[])

Your function definition shall not be compiled.
Topic archived. No new replies allowed.