Passing an array as argument to a function (2)
-
Just to clarify over on pointer/array as argument One way is to pass the array as pointer
int Function( int * arrayargument)
{
for(int i =0; i < 10; i++)
{
arrayargument[i] = 1;
}}
int AnArray[10];
//case1
Function(AnArray);
//case2
Function(&AnArray[0]);could same result be achieved by calling it case2 mode?
-
Just to clarify over on pointer/array as argument One way is to pass the array as pointer
int Function( int * arrayargument)
{
for(int i =0; i < 10; i++)
{
arrayargument[i] = 1;
}}
int AnArray[10];
//case1
Function(AnArray);
//case2
Function(&AnArray[0]);could same result be achieved by calling it case2 mode?
-
Just to clarify over on pointer/array as argument One way is to pass the array as pointer
int Function( int * arrayargument)
{
for(int i =0; i < 10; i++)
{
arrayargument[i] = 1;
}}
int AnArray[10];
//case1
Function(AnArray);
//case2
Function(&AnArray[0]);could same result be achieved by calling it case2 mode?
I would also pass the size of the array to the function instead of hard-coding it at
10
:int Function(int* arrayargument, size_t size)
Robust Services Core | Software Techniques for Lemmings | Articles
-
Yes, in case 1 the compiler assumes the name of the array actually means a pointer to the first element. In case 2 you have specifically requested "pass the address of the first element of the array".
Thanks When you can do the same thing in several ways something is not right :)
-
Thanks When you can do the same thing in several ways something is not right :)
fearless_ wrote:
When you can do the same thing in several ways something is not right
I can only imagine the following program will blow your mind:
#include int main()
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7};
int i1 = a[2];
int i2 = 2[a];printf("i1 = %d i2 = %d\\n", i1, i2); return 0;
}
Keep Calm and Carry On
-
I would also pass the size of the array to the function instead of hard-coding it at
10
:int Function(int* arrayargument, size_t size)
Robust Services Core | Software Techniques for Lemmings | Articles
I was trying to keep the example as simple as possible. But since we`re here, what`s size_t? It`s the second time I see someone using it, is it an actual variable or just a custom?
-
I was trying to keep the example as simple as possible. But since we`re here, what`s size_t? It`s the second time I see someone using it, is it an actual variable or just a custom?
size_t is just a type. Like int, long, and so on
-
size_t is just a type. Like int, long, and so on
Thanks Victor you mean a built-in type in a c++ compiler? what makes it particular, with float the compiler will let you use the comma, char is a like a tiny int, what makes size_t special?
-
Thanks Victor you mean a built-in type in a c++ compiler? what makes it particular, with float the compiler will let you use the comma, char is a like a tiny int, what makes size_t special?
[std::size_t - cppreference.com](https://en.cppreference.com/w/cpp/types/size\_t) [size_t - cppreference.com](https://en.cppreference.com/w/c/types/size\_t) [size_t c - Google Search](https://www.google.com/search?q=size\_t+c%2B%2B&oq=size\_t&aqs=chrome.1.69i57j0l7.14987j0j7&sourceid=chrome&ie=UTF-8)
-
[std::size_t - cppreference.com](https://en.cppreference.com/w/cpp/types/size\_t) [size_t - cppreference.com](https://en.cppreference.com/w/c/types/size\_t) [size_t c - Google Search](https://www.google.com/search?q=size\_t+c%2B%2B&oq=size\_t&aqs=chrome.1.69i57j0l7.14987j0j7&sourceid=chrome&ie=UTF-8)
oh so it`s a class object from a library not a built in type.
-
oh so it`s a class object from a library not a built in type.
fearless_ wrote:
oh so it`s a class object from a library not a built in type.
:zzz: :confused: :zzz: One more link: [size_t](https://www.viva64.com/en/t/0044/)
-
fearless_ wrote:
oh so it`s a class object from a library not a built in type.
:zzz: :confused: :zzz: One more link: [size_t](https://www.viva64.com/en/t/0044/)
Are you bored? don`t be, I`m learning
-
Are you bored? don`t be, I`m learning
Don't worry! Just learn! :) And sorry if I wrote something wrong here!
-
Don't worry! Just learn! :) And sorry if I wrote something wrong here!
ok