简体   繁体   English

C中的字符串如何分配指向char的指针?

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

I am new to C/C++.我是 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.我知道第一个是指针,第二个是字符数组。但是,我不明白 char*x 是如何工作的。

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? &a 是 memory 地址。b 是指针。但是,“hello world”的 memory 地址是什么。它如何应用于 x 指针?谁能解释一下?

In C all literal string are stored as non-modifiable (but not constant) arrays of characters, including the null-terminator.在 C 中,所有文字字符串都存储为不可修改(但不是常量)的 arrays 字符,包括空终止符。

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).您将x初始化为指向此类数组的第一个元素(请记住,arrays 衰减为指向其第一个元素的指针)。

It's similar to:它类似于:

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

But with the difference that the string in this example is modifiable.但不同的是,本例中的字符串是可修改的。

What is the memory address for "hello world" ? "hello world"的 memory 地址是什么?

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.字符串文字"hello world"的确切地址由操作系统在执行时确定,并且还取决于您的实现和资源。 A program has no influence on the exact location of a string literal in memory.程序对 memory 中字符串文字的确切位置没有影响。

"How it apply to the pointer x ? Can anyone explain a little bit"? “它如何应用于指针x ?谁能解释一下”?

char *x = "hello world";

x is a pointer to char (as you already know); x是指向char的指针(如您所知); "hello world" is a string literal stored in read-only memory and isn't modifiable. "hello world"是存储在只读 memory 中的字符串文字,不可修改。

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.使用此语句,指针x由只读 memory 内部的此字符串文字的第一个元素的地址(实际上是h )初始化。

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? char s[] 和 char *s 有什么区别?

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

https://en.wikipedia.org/wiki/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.如果 x 仅应指向字符串文字,则将x设为指向const char ( const char *x ) 的指针。 In that way, the program will never invoke any undefined behavior by any unintentional write attempt to modify a string literal.这样,程序将永远不会通过任何无意的写入尝试来修改字符串文字来调用任何未定义的行为。

Let us take an example;让我们举个例子;

char *b = "john";字符 *b = "约翰"; 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是一个指针变量。(b)的大小是4个字节,你要明白一点p和&p是不一样的,在上面的例子中b是存放在栈中但“john”存放在的代码段memory,char *b =“约翰”; b=petter;b++ is a valid one.char *b = "john"; b=petter;b++ 是一个有效的 one.char *b = "john"; b[0]=n. b[0]=n。

The statement 'char *b = “john” creates a string literal.语句 'char *b = “john” 创建一个字符串文字。 The string literal is stored in the read-only part of memory by most of the compilers.大多数编译器将字符串文字存储在 memory 的只读部分中。 The C and C++ standards say that string literals have static storage duration, any attempt at modifying them gives undefined behavior. C 和 C++ 标准说字符串文字具有 static 存储持续时间,任何修改它们的尝试都会给出未定义的行为。

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.我认为您在这里缺少的是,如果您创建一个数组,则基本上会创建一个指向 Memory 中第一个元素的指针。 (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 (这个页面总结得很好: 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 .) 那又怎样

    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.所做的就是创建字符串“hello world”并将第一个字符的位置存储在指针变量 x 中。 That is just the same as那是一样的

    char x[]="hello world";

Because of this, following code produces the same output for x and y:因此,以下代码为 x 和 y 生成相同的 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM