• Quick C++ char question
    16 replies, posted
So, i've been programming c++ for a while now. I haven't been reading any programming books, only some internet tutorials. My question is this: [code] char* chr; const char* chr; char chr[]; std::string; //From iostream [/code] what's the difference and which one is the best one to use? I'm pretty sure 'const' stands for 'constant', but not really sure. And if so.. Why would you want a constant char? I've seen alot of functions returning 'char*'. I can almost guess that the * has something to do with arrays.
char* I believe is the same as char[]. Const stands for constant and means it doesn't change. Finally, std::string has a lot more functions and that stuff.
char[] is interchangeable with char*
[QUOTE=danharibo;22519193]char[] is interchangeable with char*[/QUOTE] So, it's about the same?
An array in C++ is a pointer to the first element of a sequence. As such, an array is actually the same as a pointer. You want const char* when you have a string, which contents you do not wish to be modified. std::string is the C++-version of a string. char* is the C-version, though as well supported by C++. It's usually right to use std::string when you want a string in C++.
The perfect answer. Thanks!
You can even use pointers to access elements of the array and this will be a bit more efficient. Only thing is that pointers can get a little hairy if mess up.
[QUOTE=danharibo;22519193]char[] is interchangeable with char*[/QUOTE] This is just wrong. [QUOTE=ZeekyHBomb;22519409]An array in C++ is a pointer to the first element of a sequence. As such, an array is actually the same as a pointer.[/QUOTE] An array is not the same as a pointer. A dynamically allocated array will return "garbage" when you use sizeof() on it (basically, it returns the size of the pointer), while an actual array will return the expected result. See this: [URL]http://codepad.org/8aUpK7E8[/URL] And this: [URL]http://codepad.org/M53a26SP[/URL] And this: [URL]http://codepad.org/u7iDLzou[/URL] [QUOTE=Pepin;22519808]You can even use pointers to access elements of the array and this will be a bit more efficient.[/QUOTE] What? How? [cpp] char text[] = "text"; // This: std::cout << text[0]; // Should result in the exact same code as this (other than the obvious pointer creation if the compiler doesn't want to optimize it out): char* c_ptr = text; std::cout << *c_ptr; [/cpp] Someone please error-check me. I'm pretty sure I'm correct on this.
oh right, thanks for clearing it up. I had always seen arrays and pointers used together.
[QUOTE=danharibo;22520052]oh right, thanks for clearing it up. I had always seen arrays and pointers used together.[/QUOTE] They have some huge similarities, but they still have some differences that IMO exclude the term "same" from being applied to them. The stuff I showed above will probably not matter much in a real program, however.
Welcome back g.
[QUOTE=efeX;22520492]Welcome back g.[/QUOTE] Hi. Got a little bored and no SC2 now so might be posting a few times.
Arrays and pointers are distinct types, but an array can be implicitly converted into a pointer to its first element, so the two are related. An array has a size as part of its type (e.g. char[4] and char[10] are two different types) whereas a pointer doesn't. The sizeof() operator on an array returns the actual total size of all the elements in the array; the sizeof() operator on a pointer just returns the size of the pointer, as gparent said, because the pointer has no information about the number of data items (if any) stored at the location it points to. An array can potentially be more efficient than a std::string in certain cases, because the latter relies on dynamic allocation and the former doesn't. However, string manipulation is unlikely to be the limiting factor in a program's performance, and using raw arrays can be [i]less[/i] efficient if you don't know what you're doing, and in any case they're trickier to use correctly (leading to bugs) and don't provide all the useful features that std::string does. Use std::string unless you have a good reason to use a char array. The difference between a char* and a const char* is that the latter doesn't allow the characters to be modified through it. You should read about [url=http://en.wikipedia.org/wiki/Const-correctness]const-correctness[/url], and in particular the [url=http://www.parashift.com/c++-faq-lite/const-correctness.html]C++ FAQ section on const-correctness[/url], for information on how to use "const" properly. BTW, std::string comes from <string>, not <iostream>. Your <iostream> might happen to include <string> for you as part of its implementation, but you shouldn't rely on that; #include <string> yourself.
[QUOTE=gparent;22519929]What? How? [cpp] char text[] = "text"; // This: std::cout << text[0]; // Should result in the exact same code as this (other than the obvious pointer creation if the compiler doesn't want to optimize it out): char* c_ptr = text; std::cout << *c_ptr; [/cpp] Someone please error-check me. I'm pretty sure I'm correct on this.[/QUOTE] Well since an array is a pointer, you can use it exactly like a pointer. My teacher gave us an example by using making a strcmp function using only pointers. I'm a bit rusty with C, so I'll let this response do all of the talking. [quote]The code [cpp]int array[3];[/cpp] is the same as [cpp]int const *array=new int[3];[/cpp] And the subscript operator ([]) is really a dereference operator with a shift, so [cpp]array[2];[/cpp] is really [cpp]*(array+2);[/cpp] Because of pointer arithmetic, adding X to an address of type T is the same as shifting that address by X*sizeof(T). The handling of arrays as pointers is very crucial to an understanding of C. For example, you can pass an array anywhere a pointer is expected, and vice versa. A function can handle arrays, but define the type it recieves as a pointer. And since arrays are pointers (constant pointers), they are passed call by reference, meaning the array passed will be modified immediately in the calling function.[/quote] I'm not sure if your code would work, but doing this should. [cpp] const char text[] = "text"; //outputs t std::cout << text[0]; std::cout << *text; //outputs x std::cout << text[2]; std::cout << *(text+2); [/cpp]
haha wow, you need a teacher who knows C++. [editline]07:24PM[/editline] Also new keyword isn't in C.
A stack allocated array is definitely not the same as a dynamically allocated array, no matter what your CS teacher says. [] is the same as *(ptr + i) yes, but that has nothing to do with an array being similar to a pointer or not, that just has to do with [] being built that way.
C lets you do this [cpp]5[myArray][/cpp] :psyduck:
Sorry, you need to Log In to post a reply to this thread.