This question already has answers here:
What is array to pointer decay?
(11 answers)
why is array name a pointer to the first element of the array?
(3 answers)
Is an array name a pointer?
(8 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 3 months ago.
I tried to build a function that takes an array and its size and returns an array with only the even numbers from the first array.
#include <iostream>
using namespace std;
int noOdds(int v[], int dim)
{
int vp[dim];
for (int i = 0, k = 0; i < dim; i++)
{
if (v[i] % 2 == 0)
{
vp[k] = v[i];
k++;
}
}
return vp;
}
int main()
{
int v1[3] = { 1,2,3 };
cout << noOdds(v1, 3);
return 0;
}
The error I am getting:
main.cpp: In function ‘int noOdds(int*, int)’:
main.cpp:21:12: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
21 | return vp;
| ^~
| |
| int*
I kept looking on the internet for different ways to solve this error, but I didn't find anything, I would be very grateful if someone could help me. Thank you!!
Related
This question already has answers here:
Why is <iterator>'s std::size not working with raw array parameters
(1 answer)
Why does the std::size on an array passed by value do not work?
(3 answers)
What is array to pointer decay?
(11 answers)
Closed 4 months ago.
Beginner in c++ here.
why do i get the following error message when i run this code?
mismatched types 'const _Tp [_Nm]' and 'int*'
#include <bits/stdc++.h>
using namespace std;
void f(int arr[]){
int arr_size = size(arr);
for (int i = 0; i < arr_size; i++){
cout << arr[i] << " ";
}
}
int main(){
int array[5] = {1,2,3,4,5};
f(array);
return 0;
}
I tried this and it works as expected.
#include <bits/stdc++.h>
using namespace std;
int main(){
int array[5] = {1,2,3,4,5};
int arr_size = size(array);
for (int i = 0; i < arr_size; i++){
cout << array[i] << " ";
}
return 0;
}
Can someone explain what went wrong and how to fix it? Thanks.
This question already has answers here:
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 7 months ago.
What is the difference between taking as a function argument an int pointer or an int array in C++?
void arrayFunction1(int * x) {
for(int i = 0; i < 10; i++) {
cout << x[i] << endl;
}
}
void arrayFunction2(int x[]) {
for(int i = 0; i < 10; i++) {
cout << x[i] << endl;
}
}
int main() {
int dstdata[10];
arrayFunction1(dstdata);
arrayFunction2(dstdata);
return 0;
}
Both results look the same to me.
There are completely identical. In a function parameter int x[] is just another way of writing int* x. Even int x[10] is the same (the 10 is completely ignored).
Now why Kernighan and Ritchie (the inventors of C) thought this piece of deception was a good idea is another question. I guess they weren't thinking of all the people who have to learn the syntax.
This question already has answers here:
The program doesn't give any output on VS Code if I use 'const int ' and assign it a big value like 1e6 [duplicate]
(1 answer)
Why is this C++ program crashing? [duplicate]
(2 answers)
C++ program is not giving output involving large array
(5 answers)
Closed 9 months ago.
Whenever is give N = 1e6 + 2 like large value to N. VS Code doesn't work and on runtime shows .exe stopped working. Couldn't figure out why happening
#include <iostream>
using namespace std;
int main()
{
int n, arr[n];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int N = 1e6 + 2;
bool check[N];
for (int i = 0; i < N; i++)
{
check[i] = false;
}
return 0;
}
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 1 year ago.
I'm trying to make a function that returns the length of an array, but the function always returns 8
#include <iostream>
using namespace std;
int size(int arr[])
{
return sizeof(arr);
}
int main()
{
int v[] = {1, 2, 4};
cout << sizeof(v) << endl;
cout << size(v);
return 0;
}
This is because ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int*’..
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 8 years ago.
I am using sizeof trick to get the length of array, but it only looks like it's adding 1 and 2.
#include <iostream>
#include <stdio.h>
using namespace std;
int add(int array[])
{
int sum = 0;
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i += 1)
{
sum += array[i];
}
return sum;
}
int main()
{
int array[4] = {1, 2, 3, 4};
cout << add(array);
return 0;
}
Output is 3. What is wrong?
In a parameter to a function, int array[] is another way of saying int *array, so sizeof(array) will return the size of a pointer-to-int. I’m pretty sure there’s a more idiomatic C++ way of doing this, particularly in the newer versions of C++, but the C way of dealing with this would be to pass a second parameter with the size of the array.