简体   繁体   English

我是否在 C 中使用指针变量将值直接分配给 memory 位置?

[英]Am I assigning a value directly to a memory location with a pointer variable in C?

I've decided to complicate my life by learning C.我决定通过学习 C 来复杂化我的生活。 Currently I am trying to fully understand pointers.目前我正在尝试完全理解指针。

So far this is what I understand:到目前为止,这是我的理解:

#include <stdio.h>
int main() {

    char var = 'A';
    // Iniialize a pointer variable to the ADDRESS of variable "var"
    char *ptr = &var;
    printf("%c", var);    // Output: 'A'

    // Change the VALUE at the memory location of variable "var"
    *ptr = 'B';
    printf("%c", var);    // Output: 'B'

    return (0);
}

But then I was looking into re-assigning a string to a variable, and came across this post , where a pointer variable is assigned a value directly.但是后来我正在考虑将一个字符串重新分配给一个变量,并遇到了这篇文章,其中一个指针变量被直接赋值。

This is what I am trying to understand:这就是我想要理解的:

#include <stdio.h>
int main() {

    // Declare a pointer variable
    char *ptr;

    // Assign a VALUE directly to a memory location???
    ptr = "String 1";
    printf("%s", ptr);    // Output: "String 1"

    return (0);
}

Also, if I assign a new string value to ptr , such as ptr = "String 2";另外,如果我为ptr分配一个新的字符串值,例如ptr = "String 2"; , and also output the memory location after each assignment, I get a different memory location: ,还有 output memory 位置在每次分配后,我得到一个不同的 memory 位置:

#include <stdio.h>
int main() {

    // Declare a pointer variable
    char *ptr;

    // Assign a VALUE directly to a memory location???
    ptr = "String 1";
    printf("%s\n", ptr);    // Output: "String 1"
    printf("%p\n", ptr);    // Output: 0000000000404003 <- one memory location

    // Assign a new VALUE to the pointer variable
    ptr = "String 2";
    printf("%s\n", ptr);    // Output: "String 2"
    printf("%p", ptr);    // Output: 000000000040400F <- a new memory location

    return (0);
}

Am I really assigning a VALUE to the memory location the pointer variable is referencing?我真的为指针变量引用的 memory 位置分配了一个值吗?

Why does the memory location change when I assign a new value to the pointer variable?为什么当我为指针变量分配新值时,memory 位置会发生变化?

pointer hold reference to (address of) the object. pointer保存对 object (地址)的引用。

char *ptr = "string 1";

is the same as是相同的

char *ptr;
ptr = "string 1";

it assigns the pointer ptr with the address of the string literal "string 1" .它为指针ptr分配字符串文字"string 1"的地址。 String literals are null character terminated const char arrays.字符串文字是 null 字符终止的const char arrays。

When you:当你:

ptr = "string 2";

you assign the pointer ptr with the address of another string literal.您将指针ptr分配给另一个字符串文字的地址。 And as I wrote above is simply a const char array.正如我上面写的,它只是一个const char数组。 Addresses of both string literals are different.两个字符串文字的地址不同。

 ptr = "String 2"; printf("%s\n", ptr); // Output: "String 1"

It is not possible, and you post here different code than you run on your computer.这是不可能的,并且您在此处发布的代码与您在计算机上运行的代码不同。 It will print String 2 for sure.它肯定打印String 2

 // Assign a VALUE directly to a memory location??? ptr = "String 1";

No, this assignment only assigns pointer ptr with the address of string literal which resides somewhere in memory.不,此分配仅将指针ptr分配给位于 memory 某处的字符串文字的地址。 It does not allocate the space or copy this string anywhere.它不会在任何地方分配空间或复制此字符串。

The magic * :魔法*

When you use the dereference operator * you access the object the pointer is referencing.当您使用解引用运算符*时,您访问的是 object 指针正在引用。

char str[] = "String 1";
char *ptr;

ptr = str;
*ptr = 'X';

printf("str = `%s` And its address is %p\n", str, (void *)str);
printf("ptr = `%s` it is holging %p address. Its address is %p\n", ptr, (void *)ptr, (void *)&ptr);

Output: Output:

str = `Xtring 1` And its address is 0x7ffdc9047827
ptr = `Xtring 1` it is holging 0x7ffdc9047827 address. Its address is 0x7ffdc9047818

As you see it changes the first character of the str as pointer ptr holds the address of the first character of this array.如您所见,它更改了str的第一个字符,因为指针ptr保存了该数组第一个字符的地址。

https://godbolt.org/z/x8PsW9vsP https://godbolt.org/z/x8PsW9vsP

To assign a value to an object using a pointer that points to the object you need to dereference the pointer as you are doing in the first your program.要使用指向 object 的指针为 object 分配值,您需要像在第一个程序中所做的那样取消引用该指针。

*ptr = 'B';

As for this code snippet至于这个代码片段

char *ptr;

ptr = "String 1";

then there is declared a pointer that then is assigned with the address of the first character of the string literal "String 1" .然后声明一个指针,然后为该指针分配字符串文字"String 1"的第一个字符的地址。

You can reassign the pointer like您可以重新分配指针,如

ptr = "String 2";

but this reassigns the pointer itself not the object previously pointed to by the pointer.但这会重新分配指针本身,而不是指针先前指向的 object。 That is now the pointer points to the first character of the string literal "String 2" .现在,指针指向字符串文字"String 2"的第一个字符。

Pay attention to that you may not change the string literal using the pointer like for example请注意,您可能不会使用指针更改字符串文字,例如

*ptr = 's';

Any attempt to change a string literal results in undefined behavior.任何更改字符串文字的尝试都会导致未定义的行为。

But you can change a character array initialized by a string literal like但是您可以更改由字符串文字初始化的字符数组,例如

char s[] = "String 1";
char *ptr = s;
*ptr = 's';

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

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