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.
Related
This question already has answers here:
getting size of array from pointer c++
(6 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
const int triTable[4][6] = {
{},
{0, 8, 3},
{0, 1, 9},
{1, 8, 3, 9, 8, 1},
};
int main() {
const int *f = triTable[3];
int size = sizeof(f)/sizeof(*f);
cout << size;
}
This gives me wrong numbers. It should print 6. How can I get it?
How to get size of const array in c++?
Like this:
std::size(triTable[3])
This gives me wrong numbers.
You generally cannot get the size of an array by using a pointer to element of that array.
As stated sizeof f is the size of the pointer not the size of the object where it points to, as also said, using STL containers is a much better option given that they have size members that keep track of the size of the container.
That being said, if you're adamant in using a C style array, a solution would be to wrap it in a struct or class and return the size from there, i.e:
struct myArray
{
const int triTable[4][6] = {
{},
{0, 8, 3},
{0, 1, 9},
{1, 8, 3, 9, 8, 1},
};
const int size = std::size(triTable[3]); // size data member
int arraySize() const{
return std::size(triTable[3]); // size method
}
};
int main()
{
myArray a;
std::cout << a.size << "\n";
std::cout << a.arraySize();
}
Output:
6
6
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:
How to sum up elements of a C++ vector?
(13 answers)
Closed 2 years ago.
say I have an array that is an integer and stores five numbers, I want to add these numbers together and put them into a certain variable, how do I do this. if you have the answer to this then it would be greatly appreciated if you could respond. thanks.
Here is a simple code to sum integers:
Try it online!
#include <vector>
#include <iostream>
int main() {
std::vector<int> a = {1, 2, 3, 4, 5};
int res = 0;
for (auto x: a)
res += x;
std::cout << res << std::endl;
return 0;
}
Output:
15
Alternative way is to use std::accumulate:
Try it online!
#include <vector>
#include <iostream>
#include <numeric>
int main() {
std::vector<int> a = {1, 2, 3, 4, 5};
auto res = std::accumulate(a.begin(), a.end(), 0);
std::cout << res << std::endl;
return 0;
}
This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 6 years ago.
#include <iostream>
using namespace std;
int ROWS = 3;
int COLS = 4;
How do I solve this c++ multidimensionalArray problem? I have been working on this for some time but I just cant figiure it out, thank you verry much
void fillScores(int [ROWS][COLS]);
int main() {
int scores[ROWS][COLS] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
fillScores(scores);
return 0;
}
void fillScores(int newScores[ROWS][COLS]) {
cout << newScores[1][1]<<endl;
}
You can wrap the array in a struct and then pass it by address, where you need.
struct ArrayWrapper
{
int _arr[ROWS][COLS];
};
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.