0

The following piece of code works fine. Problem is I need it to work when size of array is unknown. In the example below I have hardcoded the values to 2. In the real world I do not know the size. Is there a way to modify the code so that it works even when size of the array is not known.

void namesArray(std::string (&numList)[2], std::string name)
{
    //This is just place holder code. Please ignore the logic. 
    numList[ 0 ] = "Peter" + name;
    numList[ 1 ] = "Bruce" + name;
}

int main()
{
    std::string nameList[2];
    namesArray( nameList, "Parker");
    std::cout << nameList[0]<< std::endl;
    std::cout << nameList[1] << std::endl;
    return 0;
}

I CANNOT use any other datatype (eg: Vectors) except Arrays due to external limitations.

Edit: When I say the size is unknown, what I mean is the size of the Array is not known until runtime. Also, what I am presenting is an over simplification of my actual code. The function accepts only arrays.

UPDATE: Thank you all for the solutions offered. Looks like the code I have already authored worked in my solution. I know it's wierd to use arrays when vetors offer more flexibility. However, when dealing with legacy code you sometimes don't have a choice. THANKS A LOT FOR ALL THE ANSWERS TO EVERYONE WHO RESPONDED. IT WAS VERY INFORMATIVE.

8
  • 1
    Where does the size come from? Commented Jul 3, 2018 at 5:31
  • Can you pass in the size of the array as a second parameter? Commented Jul 3, 2018 at 5:32
  • 1
    "I do not know the size" might mean two different things. Either you (the programmer that writes namesArray) don't know the size, but it is known statically to the caller; or the size is not known to anyone until run time. Commented Jul 3, 2018 at 5:35
  • Prefer to use std::array in a case like that. Commented Jul 3, 2018 at 5:49
  • The size is not known at the time the function is called. It is not known to anyone until run time. Commented Jul 3, 2018 at 5:54

3 Answers 3

2

If I understood your question well, you can use template mechanism to array size deduction:

#include <iostream>
#include <string>

template <size_t N>
void namesArray(std::string (&numList)[N], std::string name) {
    //This is just place holder code. Please ignore the logic.
    numList[ 0 ] = "Peter" + name;
    numList[ 1 ] = "Bruce" + name;
}

int main() {
    std::string nameList[5];
    namesArray( nameList, "Parker");
    std::cout << nameList[0]<< std::endl;
    std::cout << nameList[1] << std::endl;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You got presented several solutions. I'd like to present another, one that is an abstraction which incorporates several solutions. Take a gsl::span (or a std::span if you are from the future).

A span is generalized a view on a contiguous sequence of elements. And a powerful abstraction.

You want to pass an array of static size? A span can be constructed from one via a template constructor.
You want to pass a pointer and a size? span got you covered there as well.
A container like std::vector or std::array? No problem.

Use a span if all you care about is the sequence property, and not what the sequence itself is.

2 Comments

Thank you for suggesting span. The order of the sequence in which data is stored itself does matter. Is span a part of std?
@JayHawk -The proposal was adopted for C++20. So not in std yet. The gsl implementation is available for a while. It's also a fairly simple to implement your own reduced version of span. All you need is a class that holds a pointer and size, and has a bunch of constructors overloaded for the various cases. You can limit yourself to overloaded constructors like the other answers mention (a template for static arrays, and a pointer + size otherwise).
-1

The simplest solution is to pass the size in with the array:

void namesArray(std::string *numList, std::size_t numCount, std::string name)

This will probably work for you -- you need to know the size of the array when you pass it in, but it doesn't require that you be working with a stack array from that scope, and in general usage you're more likely to have access to the size of the array than to be creating the array in the same scope that you're calling the function. It also makes things much more explicit and, if you switch to heap arrays instead of stack arrays, it still works fine.

Here's the thing: You always know the size of the array. It's literally impossible to write code that creates an array of an unknown size. You can (un)intentionally forget the array size, but at some point, it has to be known, because you literally can't create the array otherwise. You might only know it at runtime, because it's, say, defined by user input, but that just means it's in a variable, and you still know it, you just don't have it predefined at compile time.

Also, if the array size is defined at runtime, you're using heap arrays, and the other solution won't work for that.

2 Comments

The function is called at run time and hence I do not know the size of the array at that time.
@JayHawk The function is called at run time and hence I do not know the size of the array at that time Whoever is calling your function must have the size available at run time. Otherwise they cannot use their array at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.