Janith
Janith

Reputation: 413

How is a pointer to char assigned by a string in C?

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?

Upvotes: 1

Views: 1157

Answers (4)

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.

Upvotes: 0

Crane Ious
Crane Ious

Reputation: 11

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);

Upvotes: 1

user13712259
user13712259

Reputation:

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.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409356

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.

Upvotes: 0

Related Questions