1

I am new to C/C++.

char *x="hello world";
char x[]="hello world";

I know first one is a pointer and second one is a character array.but,I can't understand how char*x works.

int a=1;
int *b=&a;

&a is the memory address.b is the pointer.but,what is the memory address for "hello world".how it apply to x pointer?can anyone explain a little bit?

Janith
  • 403
  • 6
  • 14
  • 4
    Please don't use the term "C/C++". There no such language, only the two separate, distinct and *very different* languages C and C++. Especially when it comes to strings and string literals there is actually a big difference between the two languages. – Some programmer dude Jun 09 '20 at 08:47
  • 2
    `char *x="hello world";` is invalid in C++, so, because of that - it doesn't work. – Algirdas Preidžius Jun 09 '20 at 08:48
  • 1
    Does this answer your question? [What is the difference between char s\[\] and char \*s?](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – Abhishek Bhagate Jun 09 '20 at 08:52

4 Answers4

1

Short answer: they are basically the same.

I think what you are missing here is that if you create an Array, basically a pointer to the first element in Memory is created. (This page sums it up pretty nicely: https://www.studytonight.com/c/pointers-with-array.php#:~:text=Pointer%20and%20Arrays%20in%20C,also%20allocated%20by%20the%20compiler.&text=We%20can%20also%20declare%20a,point%20to%20the%20array%20arr%20.) So what

    char *x="hello world";

does, is to create the string "hello world" and store the location of the first char in the pointer variable x. That is just the same as

    char x[]="hello world";

Because of this, following code produces the same output for x and y:

    char *x= "hello world";
    char y[] = "hello world";

    printf("x[0]: %c \n", x[0]);
    printf("y[0]: %c \n", y[0]);

    printf("x: %s \n", x);
    printf("y: %s \n", y);
Crane Ious
  • 11
  • 2
0

In C all literal string are stored as non-modifiable (but not constant) arrays of characters, including the null-terminator.

When you do:

char *x = "hello world";

you initialize x to point to the first element of such an array (remember that arrays decays to pointers to their first element).

It's similar to:

char s[] = "hello world";
char *x = s;

But with the difference that the string in this example is modifiable.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • that means x equal to memory address of 'h'? Can you explain more? – Janith Jun 09 '20 at 08:59
  • @Janith Yes that's correct. Using my example with `s`, `x == &s[0]` will be true. – Some programmer dude Jun 09 '20 at 09:01
  • Not,I mean ,if I call something like 's' in C.it will return a memory address? as you said,that string is in memory,but ,how it's address automatically assign to a pointer without calling a array or & operator(I mean there are no arrayname)?Can you explain?thanks... – Janith Jun 09 '20 at 10:05
  • @Janith Yes, in C arrays decays to pointers to their first element. Using plain `s` is exactly equal to `&s[0]`. I.e. it's a `char` pointer (`char *`) to the first character in the array, which is the `'h'`. – Some programmer dude Jun 09 '20 at 10:08
  • I know that,there is no array name for that array which store in memory(I mean "hello world").then,how get a memory address? char y[]="hello world"; this has a variable name y,and y has assigned memory address of area which stored 'h'(stack area).but ,it is not for char *y,noname for array,then how it is assigned to a pointer?can you explain? – Janith Jun 09 '20 at 10:15
  • @Janith Ah you mean the string literal itself? Then the compiler creates its array somewhere in memory, so it's loaded with the rest of the program. Exactly where it's stored is irrelevant, all you need to know is that all string literals are arrays, and you're not allowed to modify the contents of string literals (it leads to *undefined behavior*). – Some programmer dude Jun 09 '20 at 10:18
  • then something like this: char y[]= "hello world"//when program load to ram,"hello world" is stored as array in code area(read only),if I think it's name is y,it return first memory address of array char *x=y //y return the first memory address of that array and that is assigned to a pointer called x(it's scope in stack area of memory) cout << x //After If I call x ,char from y address to address of null terminator(\0) will return Am I correct? – Janith Jun 09 '20 at 10:58
  • @Janith Literal strings aren't named, you can't reference them through the use of variables or symbols, only as the literal strings themselves. Other than that, as I've been saying, `char *x = "hello world";` will make `x` point to the first character of the string literal. – Some programmer dude Jun 09 '20 at 11:04
0

What is the memory address for "hello world"?

Noone can say that before the execution. The exact address of the string literal "hello world" is determined at execution by the operation system and is furthermore dependent upon your implementation and ressources. A program has no influence on the exact location of a string literal in memory.

"How it apply to the pointer x? Can anyone explain a little bit"?

char *x = "hello world";

x is a pointer to char (as you already know); "hello world" is a string literal stored in read-only memory and isn't modifiable.

With this statement, the pointer x get initialized by the address of the first element of this string literal, actually h, inside of the read-only memory.

Since the string literal automatically evaluates to a pointer to its first element, there is no need to use the & operator.

Take a look at:

What is the difference between char s[] and char *s?

https://en.cppreference.com/w/cpp/language/string_literal

https://en.wikipedia.org/wiki/String_literal


Side note:

  • Make x a pointer to const char (const char *x) if it only shall point to string literals. In that way, the program will never invoke any undefined behavior by any unintentional write attempt to modify a string literal.
0

Let us take an example;

char *b = "john"; Here b is a pointer variable.size of (b)is 4 bytes, you must understand one ting p and &p are not the same, In the above example b is stored at the stack but "john" is stored at the code section of the memory, char *b = "john"; b=petter;b++ is a valid one .char *b = "john"; b[0]=n.

The statement ‘char *b = “john” creates a string literal. The string literal is stored in the read-only part of memory by most of the compilers. The C and C++ standards say that string literals have static storage duration, any attempt at modifying them gives undefined behavior.