Int Array to Function: Passing direct value??

Is there a way to pass a direct value to a function that expects an Int Array as its input?

I'm working on a code wars problem. I have the following function.

1
2
3
std::string createPhoneNumber(const int arr [10]){
  return "test";
}


Is there a way to call the function and just input the numbers I want? I've been able to get this to work by creating an Int Array in the main function and just passing that Array, like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

std::string createPhoneNumber(const int arr [10]){
  return "test";
}

int main()
{
    int test[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    std::cout << createPhoneNumber(test) << '\n';

    return 0;
}


But I would like to do something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

std::string createPhoneNumber(const int arr [10]){
  return "test";
}

int main()
{
    std::cout << createPhoneNumber({1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) << '\n';

    return 0;
}


Can you do something like this?
Just use a std::vector<int> instead of an int[] array.
Well "it works" - and that usually means it's dodgy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>

std::string createPhoneNumber(const int arr [10])
{
    for(int i = 0; i < 10; i++)
        std::cout << arr[i] << ' ';
    std::cout << '\n';
    
    return "test";
}

int main()
{
    std::cout << createPhoneNumber( new int[]{ 23, 2, 3, 4, 15, 6, 7, 8, 9, 40} ) << '\n';

    return 0;
}
Just use a std::vector<int> instead of an int[] array.


Thanks for the advice. Unfortunately because this is a question on CodeWars. I don't have that luxury.

Well "it works" - and that usually means it's dodgy!


Nice! I just learned about New and Delete too.
I was thinking it wasn't working for me because I was using the syntax wrong somewhere. Looks like I'm better off just creating a variable for it.

Thank you for your replies ^_^
Last edited on
Is there a way to pass a direct value to a function that expects an Int Array as its input?
There isn't. Function parameters never have array type. For example the function declaration
std::string createPhoneNumber(const int arr [10])
Counterintuitively declares a function with a single parameter of type int const*.

Note that reference or pointer-to-array type is fine, but that would look like
std::string createPhoneNumber(const int (&arr) [10])
To be called like
createPhoneNumber({1, 2, 3, 4, 8, 6, 7, 5, 3, 0, 9});
Last edited on
Wait.
I have a question about using New Int.

 
std::cout << createPhoneNumber( new int[]{ 23, 2, 3, 4, 15, 6, 7, 8, 9, 40} ) << '\n';


If I don't use Delete to remove this down the line, does that mean every time I run this program I take memory from the OS and never give it back? I'm just curious.
If I don't use Delete to remove this down the line, does that mean every time I run this program I take memory from the OS and never give it back? I'm just curious.

The systems you're used to using are designed to recover any memory that was allocated to but not released by a process when that process is terminated.

So many common operating systems will recover the memory when your program quits.

If you can guarantee the operating system provides this service, it is not necessarily bad to rely on it. Not all operating systems do this.

I have a question about using New Int.
That's fine but the way you are going about it is 'dodgy' and lucky at best.

There's absolutely no reason to go in that direction because effectively you are hard-coding a solution looking for a problem that will probably never exist. e.g. how are you going to change/input values to the dodgy array. It can be done but it's pointless when the orthodox ways are reliable and simple as long as you understand/follow the (C) rules/intent.

Memory is 'given back' when the program exits, so a delete is not needed. I can't think of a situation where a memory leak in your case would be even remotely significant. Upgrade your multi-tasking machine if the few bytes involved are going to be a drama/out of memory crash.

There is a proper way to declare, populate and pass/return C-style arrays.

BTW The old wives <vector>'s stuff isn't constructive, just another red herring. For simple jobs and especially in understanding the foundations of STL containers in software engineering and/or even legacy code C-arrays are an important and useful part of C++.

I have a question about using New Int.
That's fine but the way you are going about it is 'dodgy' and lucky at best.

There's absolutely no reason to go in that direction because effectively you are hard-coding a solution looking for a problem that will probably never exist. e.g. how are you going to change/input values to the dodgy array. It can be done but it's pointless when the orthodox ways are reliable and simple as long as you understand/follow the (C) rules/intent.

Memory is 'given back' when the program exits, so a delete is not needed. I can't think of a situation where a memory leak in your case would be even remotely significant. Upgrade your multi-tasking machine if the few bytes involved are going to be a drama/out of memory crash.

There is a proper way to declare, populate and pass/return C-style arrays.

BTW The old wives <vector>'s stuff isn't constructive, just another red herring. For simple jobs and especially in understanding the foundations of STL containers in software engineering and/or even legacy code C-arrays are an important and useful part of C++.


I promise I won't use it :-)
I promise I won't use it :-)

Psst! We aren't the C++ police.
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
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

const std::string fmt = "(***) ***-****";

std::string createPhoneNumber( const std::vector<int> &arr )
{
   std::string result = fmt;
   if ( std::count( result.begin(), result.end(), '*' ) != arr.size() ) return "Ouch!";
   unsigned p = 0;
   for ( auto e : arr )
   {
      p = result.find( '*', p );
      result[p] = '0' + e;
   }
   return result;
}

int main()
{
    std::cout << createPhoneNumber( { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ) << '\n';
    std::cout << createPhoneNumber( { 4, 2, 5, 3, 6, 4, 7, 5, 8, 6 } ) << '\n';
    std::cout << createPhoneNumber( { 4, 2, 5                      } ) << '\n';
}


(123) 456-7890
(425) 364-7586
Ouch!
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
36
37
38
#include <iostream>

std::string createPhoneNumber(int arr[], size_t sz) // 1 OF MANY LEGAL VARIATIONS
{
    if(sz != 10) // ?? IS ASSUMPTION CORRECT
        return "Invalid number";
    else
    {
        std::string ph_no;
        
        for(int i = 0; i < sz; i++)
            ph_no += std::to_string( arr[i] );
        
        ph_no.insert(3,"-");
        ph_no.insert(7,"-");
        
        return ph_no;
    }
}

int main()
{
    int test1[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    std::cout << createPhoneNumber(test1, sizeof(test1)/sizeof(int)) << '\n';
    
    int test2[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::cout << createPhoneNumber(test2, sizeof(test2)/sizeof(int)) << '\n';
    
    int ndx{0};
    std::cout << "Change the 5th number to : ";
    std::cin >> ndx;
    
    test1[4] = ndx;
    std::cout << createPhoneNumber(test1, sizeof(test1)/sizeof(int)) << '\n';
    
    return 0;
}



123-456-7890
Invalid number
Change the 5th number to : 8
123-486-7890
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>
#include <sstream>

std::string createPhoneNumber( int A[10] )
{
   std::stringstream ss;
   ss << "(" << A[0] << A[1] << A[2] << ") " << A[3] << A[4] << A[5] << "-" << A[6] << A[7] << A[8] << A[9];
   return ss.str();
}

int main()
{
   int A[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };   std::cout << createPhoneNumber( A ) << '\n';
   int B[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };   std::cout << createPhoneNumber( B ) << '\n';
}


(123) 456-7890
(314) 159-2653
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include <sstream>

std::string createPhoneNumber( const int (&A)[10] )
{
   std::stringstream ss;
   ss << "(" << A[0] << A[1] << A[2] << ") " << A[3] << A[4] << A[5] << "-" << A[6] << A[7] << A[8] << A[9];
   return ss.str();
}

int main() 
{
   std::cout << createPhoneNumber({1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) << '\n';
}
Last edited on
Topic archived. No new replies allowed.