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*’..
Related
This question already has answers here:
Passing an array by reference
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 23 days ago.
i have this code looks like the sizeof(skyline) returns always 8 and when deviding it with sizeof(int) that is 4 give me wrong array size .
The code :
int calcualteSkyLine(const int* skyline)
{
int arrSize = sizeof(skyline) / sizeof(int);
for(int i=0;i <=arrSize;i++)
{
std::cout << skyline[i] << std::endl;
}
}
int main(int argv,char** argc)
{
const int buldingArry[] = {1,3,2,1,2,1,5,3,3,4,2};
calcualteSkyLine(buldingArry);
return 0;
}
arrSize is always 8 .. therefor the array cant be printed , what is wrong here ?
if i replace the "arrSize" with 10 all works
What your code would look like in current C++ (and a little extra, since I needed a return value ;)
#include <numeric>
#include <iostream>
#include <vector>
int calcualteSkyLine(const std::vector<int>& skyline)
{
for(const int value : skyline)
{
std::cout << value << "\n";
}
// return the sum of all values (another nice thing you can do with numeric/vector)
// is to avoid (visible) for loops completely
return std::accumulate(skyline.begin(), skyline.end(), 0);
}
int main(int argv, char** argc)
{
std::vector<int> buldingArray{ 1,3,2,1,2,1,5,3,3,4,2 };
auto sum = calcualteSkyLine(buldingArray);
std::cout << "sum = " << sum;
return 0;
}
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!!
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Using sizeof on arrays passed as parameters [duplicate]
(3 answers)
Closed 1 year ago.
#include<iostream>
#include <vector>
using namespace std;
int size_f(vector<int> adj[])
{
return sizeof(adj);
}
int main()
{
vector<int> adj[] = {{0, 1}, {0, 2}, {1, 2}, {2, 3}};
cout << sizeof(adj) << endl;
cout << size_f(adj);
}
96
8
This is my program and its result. I don't understand why size_f return 8. Can you explain it for me? And how can I know size of array that is parameter of my funtion. Thank you.
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 7 years ago.
I'm trying to write a function that casts an array of ints into doubles. It takes in a constant pointer to the original int[] array (to avoid unwanted modifications?) and returns a pointer to an array of casted double[]. However, the code that I wrote doesn't seem to be working. Can anyone point out what's wrong with it?
#include <iostream>
using namespace std;
double* castToDouble(const int *input);
int main(){
int integers[] = {1,2,3,4};
double *doubles = castToDouble(integers);
cout << "numElements in doubles: " << sizeof(doubles)/sizeof(double) << endl;
for(int i = 0; i < sizeof(doubles)/sizeof(double); i++){
cout << doubles[i] << endl;
}
return 0;
}
double* castToDouble(const int *input){
// Obtain the number of elements in input.
int numElements = sizeof(input)/sizeof(int);
double *doubleAry = new double[numElements];
cout << "numElements in input: " << numElements << endl;
for(int i = 0; i < numElements; i++){
doubleAry[i] = static_cast<double>(input[i]);
}
return doubleAry;
}
The output of the program is the following:
numElements in input: 2
numElements in doubles: 1
1
The numElements' calculated seem to be arbitrary too. I'm pretty new to c++ and am unable to pinpoint the problem. Thanks in advance.
As you marked it C++, I thought this might be more idiomatic:
#include <vector>
#include <algorithm>
std::vector<double> CastToDouble(std::vector<int> const & ints)
{
auto doubles = std::vector<double>(ints.size());
std::transform(ints.begin(), ints.end(), doubles.begin(), [](int value) -> double {
return static_cast<double>(value);
});
return doubles;
}
int main(int argc, char* argv[])
{
auto values = std::vector<int>() = {
1, 2, 3, 4
};
auto doubles = CastToDouble(values);
}
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.