-3

I'm really confused about this because personally, I come from a java background, and recently began c++. So, I learnt all the basic stuff, like, printing stuff out to the screen and whatnot, and now, I learnt POINTERS. So, the person on youtube (The Cherno's C++ Pointer tutorial This was not the video where he declared the const char*, just the pointer tutorial that I followed.) I've been following was using this following statement to declare what I know as a 'string'.

const char* str = "random text here";

But, how is a char* converted into a string, and its even using the double quotation marks like a string! Also, what does a constant have to do with any of this? If I remove the const from my code it gives me an error. But, I understand what a pointer is. It is a variable that holds the memory address of another variable, so if one was to access that variable directly, they would just have to do *ptrVarName and dereferenced it. But how can a string "like this one" be a memory address? Wouldn't I have to do something like this?

char[] str = "string here";

and THEN do:

char* stringPointer = *str;

(WARNING: untested code!)

Thanks in advance. (oh and sorry if this is a really NOOBY question or the question is poorly constructed, I've just started out with c++ and stackoverflow)

EDIT: Ok, so I understand what the char* str means. It means that when you reference *str, it means that you're accessing the first character in memory. Ok, I get it now. But, what does const mean?

Ameer Ali
  • 19
  • 1
  • 4
  • 11
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You get a much more complete education from one of those then random online tutorials. – NathanOliver Jun 19 '17 at 15:24
  • 3
    If you are using C++, why aren't you using `std::string`? – Gillespie Jun 19 '17 at 15:27
  • 1
    Indeed, from `T*`, we cannot know if there is one or several objects. – Jarod42 Jun 19 '17 at 15:42

4 Answers4

1
const char* str = "random text here";

On the right hand side, the "random text here" defines a string literal which actually is an array of type const char[17] (including the null terminator character). When you assign that array to const char* str it decays to a pointer that points to the first character. You cannot modify the string literal through the pointer because string literals are stored in read-only memory, so the following would be illegal: str[0] = 'x';

char[] str = "string here";

This one is different. It defines the char array str which has the same size as the string literal on the right hand side (const char[12]). The string literal will be copied into the array str, so you will be able to modify str. In this case, it would be legal to write str[0] = 'x';.

zett42
  • 25,437
  • 3
  • 35
  • 72
0

If you declare const char* sth ="mystring" It will place mystring inthe memory and sth pointing to that its work like array but with direct access to memory

  • but you still havent answered my sub-question "what does the const mean?" – Ameer Ali Jun 19 '17 at 15:38
  • As petar velv said no difference net them – Amir Reza Mohammadi Jun 19 '17 at 15:43
  • But, Amir (heh you have the same pronounced name as me), when i use char* str = "some text"; c++ gives an error – Ameer Ali Jun 19 '17 at 15:47
  • @AmeerAli `const` is short for "constant." It means, a value that may not be modified. `const MyType foo = ...;` declares a that `foo` holds a value of type `MyType` that may not be modified. `const MyType* bar;` declares that `bar` holds a pointer to a value of type `MyType` that may not be modified. Note that in the second case, the pointer `bar` _may_ be modified. The thing that can't be modified is whatever `bar` points to. – Solomon Slow Jun 19 '17 at 16:55
  • Because "mystring" is const and it cant be put it to non const – Amir Reza Mohammadi Jun 19 '17 at 17:05
0

These are plain C-strings. A C string is always null terminated. In other words- it has '\0' at the end. So you need only the place in memory where the string starts and you can find when it ends.

For pointer arithmetic these [] brackets are only syntax sugar. str[] is the same as *str and str[1] is the same as *(str+1) only incrementing the pointer by one char (8 bits) and getting the address of the second element.

Petar Velev
  • 2,305
  • 12
  • 24
  • 3
    Ameer - you really need to just get a good book and read about pointers and pointer arithmetic. `(*str)+1` means "dereference the pointer `str` and add one to the result. `*(str+1)` means "add `1*sizeof(str)` to the pointer `str` and *then* dereference" – Gillespie Jun 19 '17 at 15:35
  • yes, i understand. I realized when I tried something in my text editor, then I understood. And anyway, I'm broke and don't have any money to buy a book ;( – Ameer Ali Jun 19 '17 at 15:43
0

C doesn't really have strings. A string is an array of characters, terminated by a nul (0). Now arrays and pointers in C are closely linked, and a char * or const char * usually points to a string, but only in the same way as other pointers usually point to arrays. An individual char * might only point to a single character, you have to know from context, just as an int * might point to one integer or an array of integers.

Because strings are handy, there's a special syntactical rule for strings literal. A string literal in quotes becomes a const char * (in fact its type is char * for backwards compatibility). So in this sense, C has strings. But all that is happening is that the string is being laid out in the data section of the program, then its address taken.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18