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.

JayHawk
  • 275
  • 1
  • 5
  • 15
  • 1
    Where does the size come from? – StoryTeller - Unslander Monica Jul 03 '18 at 05:31
  • Can you pass in the size of the array as a second parameter? – Nic Jul 03 '18 at 05: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. – n. m. could be an AI Jul 03 '18 at 05:35
  • Prefer to use `std::array` in a case like that. – Jesper Juhl Jul 03 '18 at 05:49
  • The size is not known at the time the function is called. It is not known to anyone until run time. – JayHawk Jul 03 '18 at 05:54
  • @JesperJuhl can you please elaborate. – JayHawk Jul 03 '18 at 05:54
  • The size of `std::string nameList[2];` is known at compile time, so you could use `std::array nameList;` instead. The advantages are that [https://en.cppreference.com/w/cpp/container/array](std::array) knows its `.size()`, has members like `.at()` and more and never degenerates to a pointer. It's easier and safer to use and really no more expensive than a C-style array. If size is not known at compile time, `std:: vector` would be the obvious choice instead. – Jesper Juhl Jul 03 '18 at 07:19
  • Then you need to pass the size explicitly in a separate argument. – n. m. could be an AI Jul 03 '18 at 10:59

3 Answers3

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;
}
BartekPL
  • 2,290
  • 1
  • 17
  • 34
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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • 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 Jul 03 '18 at 06:10
  • @JayHawk -The proposal was adopted for C++20. So not in `std` yet. [The `gsl` implementation is available for a while](https://github.com/Microsoft/GSL/blob/master/include/gsl/span). 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). – StoryTeller - Unslander Monica Jul 03 '18 at 06:15
-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.

Nic
  • 6,211
  • 10
  • 46
  • 69
  • The function is called at run time and hence I do not know the size of the array at that time. – JayHawk Jul 03 '18 at 06:13
  • 1
    @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. – n. m. could be an AI Jul 03 '18 at 11:01