Initializing an array with unknown size.

I'm having an issue. I googled this topic and can't seem to find the right solution.

I want to work with a char array, in my main code, that is populated in some function based on what the user inputs.

I don't know what the user is going to input, so I can't initialize the char array unless doing so with some vague size of considerable magnitude, but I don't want to do that because it's sloppy.

I've tried many different experiments but cant find to find a solution.

This was my last attempt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Function Prototypes:
char GetArray();

//Main Code
int main(){
	char MyArray = GetArray();
	int MyArraySize = sizeof(MyArray);
	cout << MyArraySize;
	cout << MyArray[0];

	return 0;

} //End Main

//Functions
char GetArray(){
	char MyArray[5] = {'I', 'N', 'P', 'U', 'T'};
	return *MyArray;
}
 
Last edited on
I want to work with a char array,


Do you have a good reason for doing that, why not std::string ? One can std::cin directly into a std::string from the keyboard.

I notice from your other topics, that you are reading a file 1 char at a time and comparing to eof. That is a huge clue that you have come from C programming into C++, and haven't yet learnt the C++ way of doing things. I don't want to sound critical: there are plenty of of us who were once in the exact same situation.
So i dont have all the code that im actually using here in this example. I am using cin to gather input from the user. Once the input has been aquired, i want it converted to characters saved to an array.

I havent come from C, ive done a little here and there. Im learning.

For my little project, the code is generating a file with random characters. I know that i dont have to read a file using get() for one character at a time. I need to read and remove a certain number of characters based on how many characters are gotten via the user input.

I need to know how to initialize an array with unknown size or some better way to do this. I know of a way to get around this.... But im curious to try another way. The way i will most likely do it is to get the user input and size first, then intialize the array right inside the main code and then send the user input, size, and array to the function, but if i do that,... Why even have a function to do the rest? This is why im trying to find another way.
https://www.cplusplus.com/doc/tutorial/basic_io/

The above is part of the tutorial on this site.

std::string is like an array of char but better: it can grow automatically; one can append strings with the + operator; can still address an individual char with a subscript just like an array. That is but a few of it's advantages.

Google the C++ STL, read up on how to use this to your advantage. One can achieve a tremendous amount just using the STL, there is a lot of stuff built in to make your life easier.
Use std::string. Then to obtain input of an unknown size until a specified char is entered (default \n) just:

1
2
3
std::string str;

std::getline(std::cin, str);


or replace std::cin with an ifstream variable to read from a file.

If for some reason you need a c-style null-terminated pointer then use str.c_str()

See http://www.cplusplus.com/reference/string/string/
Ill google that and read up on it, but much of what you are mentioning I kind of already know. I am using ifstream and ofstream for my file. Im working on using string as a char array now.....
Where is the reply with the TopCoder article?

There was another link I wanted to visit.

I read through the entire article at TopCoder and for the most part I understood a lot of what was being mentioned. I started to lose understanding the more I read though, so I have to go back and read/ do to fully remember and understand everything. That article failed to explain a few things. It would say to not do something but wouldn't explain why for some things.

There's so much to learn in terms of all that's available.

How long can a string become? Is there a limit?

Does anyone know of any other good articles that I could read about containers etc?

Thanks.

How long can a string become? Is there a limit?


The limit would be a little less the the RAM available to the process you are running. So I wouldn't worry about it: normally strings are split with std::getline on the newline character.

Does anyone know of any other good articles that I could read about containers etc?


I would start with std::string and std::vector; look at the reference material; experiment a bit - see what you can do. cppreference is really good, though it might come across as bit technical for a beginner. It's purpose is to be a reference for all cpp coders and is accurate in terms of the C++ standards.

https://en.cppreference.com/w/

Cpp Core guidelines is also very worthwhile in explaining the proper way to code C++:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

You may wish to consider buying a book, there is a list of good ones here:

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

Good Luck !!
Last edited on
Does anyone know of any other good articles that I could read about containers etc?


the containers have a lot of similar interfaces, so the big thing is to learn when to use which ones.
for that, a bit of research online can help.
https://embeddedartistry.com/blog/2017/08/23/choosing-the-right-stl-container-general-rules-of-thumb/
or maybe a flow chart:
https://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container
or this cross reference:
https://www.hackerearth.com/practice/notes/c-stls-when-to-use-which-stl/

that said, I use unordered map, vector, string, and maybe set the most. But this is largely tied into what kinds of problems you solve. most of mine involve raw iteration or random access of mostly static or nearly static data.
be aware that valarray exists and excels at math work. It is unloved in most of these overviews.

if you want to get in deep as to how they work, the names need to be boiled back to common ones instead of c++ ones.
c++ / normal people
list or forward list / linked list
unordered map / hash table
vector / array

most of the rest of them like stack are named ok. C++ lacks graph and tree but some of the containers use a tree (see the links).

finally, a math nerd word of warning: set isnt exactly a math set, vector isnt a math/physics vector (its almost a column vector in linear algebra, kinda), and so on. Dont try to apply math terms to c++ names.


The limit would be a little less the the RAM available

its probably the largest block of ram available. Strings and vectors require a solid block of ram, most of the others can be split across the whole pile, and in any case, virtual memory could mess with the numbers too. Maybe, for now, just assume you can store 10GB+ worth and if you need more than that revist the question. 10GB is a fair bit of data and today's best home computers hold 6 times that while corporate machines hold far, far more.
Last edited on
Thanks for all the information! I actually have two C++ books that I have been going through ever since I had to take a C++ class.

It's weird because it's like I understand all this, but don't. What I think the problem is that I'm missing a lot of information and what information I do have is jumbled in my head. Also, I have these weird false ideas in my head as well, such as not being able to copy all the text in a whole file to a single string container and that if I was to do something like that it would be an array instead of a string. I don't why or how I was thinking that. I had used a separate temp file to sort the contents of another file once for a past school project. That's how weird I was about it.

Everything is a lot clearer now. Im also better understanding how to read the errors and better understanding what I'm reading in the reference section of this site.

Thanks so much everyone. I feel like I'm learning this all from scratch now. weird.
an array instead of a string
a string is a wrapper for an array, of sorts. not exactly, but close enough at the conceptual level.

copy all the text in a whole file to a single string container
you would not want to do this for too large a file so it is possible that someone cautioned you against it at some point. These days, that would be multiple GB before you start to worry much.



Last edited on
Topic archived. No new replies allowed.