#What exactly is the point of char*
28 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Not sure how we break this up further really.
There are argc number of arguments passed via the command line to the application.
So there is an array of argc arguments.
Each individual argument within this array is a C string, ergo a char*.
So that makes the whole array a type of char**.
You may see alternative examples where this parameter is expressed as char*[].
They are both the same however. It's just semantics.
whats the purpose for this... this just seems so dumb
Do you understand pointers?
i understand that they point to the memory address of whatever were looking at so char* infile is pointing to the first characters address? i think
ok, gimmie a min, ill break it down for you
basically read the type as this
C represents strings as an array of chars. The type of this is char* meaning a pointer to the first char of that array, the last byte is a null byte signifying the end of the string
as an alias think of this as a seperate type str
char** or using that alias str* is the same thing just another level up, it's an array to different instances of str which are in themselves arrays
How main() is declared remains one of the few similarities between C and C++, I give you that.
You'd think C++ would have
int main(std::vector<std::string>> args)
Wouldn't you?
that would make too much sense
also probably some problems with construction of std string
yeah i was thinking of it like that, but i just dont see why we need to pass in a file name as a char instead of just a string? is it because strings are not a primitive data type?
C doesnt have a string type
C's string type is a char array
fun fact most programming languages are the same, they just lie to you and make up a string type
the first example that comes to mind is Rust. Rust's String type is literally the same as Vec<u8>. You used to be able to, (might still be able to, I havent looked or cared) use the equivalent of reinterpret_cast on a Vec<u8> and if it was valid utf8 it would be perfectly valid
yeah i understand a string can be categorized as a array of chars. but im programming in cpp i just dont see a use for it i as just curious as to why it was designed this way
It probably all just comes down to history, and a desire to be as backwards compatible with C, or, looking at it the other way, as accessible to existing C programmers. The other thought that harks back to my "history" comment above - the main interface was 1) inherited from C, 2) embedded in the language before any of the standard libraries were fully formed.
string as a general term may have a few different technical meanings, for example std::string is rather importantly distinct from char*
though arguably, std::string is a wrapper for such
but for example, we also have std::wstring which is a wrapper for wchar_t not char
frankly you probably can and probably should (if you can) ignore char* and just use std::string
if your professor requires it, then I guess you have no choice, but otherwise, use std::string
to be pedantic here, you don't "pass file name as a char". most file names are more than one character long. you receive arguments (such as file names) as so called C Strings and as noted above, this is mostly a history from C that you just have to deal with. you have little choice in the matter. but there exist libs like Boost to help with command line arguments https://www.boost.org/doc/libs/1_80_0/doc/html/program_options.html
Thanks I appreciate the explanation
@autumn grove Has your question been resolved? If so, run !solved :)
!solved
Thank you and let us know if you have any more questions!