将一个char指针解引用,当它与另一个char指针相等时。

huangapple go评论67阅读模式
英文:

Dereferencing a char pointer when its equated to another char pointer

问题

这是我的代码:

char *sptr = "abc", *tptr;

*tptr = *sptr;

printf("ch = %c\n", *tptr);

现在,当我们声明一个指针时,没有分配内存来存储字符变量。所以,这段代码确实运行,并在控制台上打印出'a'。我知道"abc"是一个字符串字面量,它是只读的内存。但当我执行*tptr = *sptr时,*tptr存储在哪里呢?

英文:

Here is my code

    char *sptr = "abc", *tptr;

    *tptr = *sptr;

    printf("ch = %c\n", *tptr);

Now, when we declare a pointer, no memory is allocated to store char variable. So, this code does run and 'a' is printed on the console. I know that "abc" is a string literal and its a read only memory. But when I execute *tptr = *sptr , where is *tptr is stored ?

答案1

得分: 3

"tptr = sptr;"将会导致未定义行为,因为你正在通过一个未定义的指针进行存储。这是一个严重的错误。相反,你可以使用"tptr = sptr;",这会将"tptr"赋予与"sptr"相同的值。

英文:

The assignment *tptr = *sptr; invokes undefined behavior, since you're storing through an undefined pointer. It's a severe bug. Instead, you can do tptr = sptr;, which gives tptr the same value as sptr.

答案2

得分: 1

你有:

     +--------+      +---+---+---+----+
sptr | 0x1234 | -->  |'a'|'b'|'c'|'
     +--------+      +---+---+---+----+
sptr | 0x1234 | -->  |'a'|'b'|'c'|'\0'|
+--------+      +---+---+---+----+                            
+--------+      
tptr | 0xABCD | -->  未指定的地址
+--------+      
'| +--------+ +---+---+---+----+ +--------+ tptr | 0xABCD | --> 未指定的地址 +--------+

你执行:

*tptr=*stpr;
     +--------+           这会导致未定义行为,因为
tptr | 0xABCD | -->  'a' 你向未指定的地址写入数据
     +--------+  

这会改变 tptr 指向的字符,但是 tptr 可以指向任何地方,你会覆盖那个可能已经被其他地方使用或者没有被使用的随机字符,从而引发未定义行为。

你可能想要做的是:

tptr=stpr;
     +--------+      +---+---+---+----+     
tptr | 0x1234 | -->  |'a'|'b'|'c'|'
tptr=stpr;
+--------+      +---+---+---+----+     
tptr | 0x1234 | -->  |'a'|'b'|'c'|'\0'|
+--------+      +---+---+---+----+   
^
+--------+        | 
tptr | 0x1234 | -------+
+--------+    
'| +--------+ +---+---+---+----+ ^ +--------+ | tptr | 0x1234 | -------+ +--------+

这将将指针 tptr 设置为与 stpr 指向的地址相同,即字符串 "abc"

英文:

You have:

     +--------+      +---+---+---+----+
sptr | 0x1234 | -->  |'a'|'b'|'c'|'
     +--------+      +---+---+---+----+
sptr | 0x1234 | -->  |'a'|'b'|'c'|'\0'|
+--------+      +---+---+---+----+                            
+--------+      
tptr | 0xABCD | -->  Unspecific address
+--------+      
'| +--------+ +---+---+---+----+ +--------+ tptr | 0xABCD | --> Unspecific address +--------+

You do

*tptr=*stpr;
     +--------+           This causes UB because
tptr | 0xABCD | -->  'a'  you write to an Unspecific address
     +--------+  

This changes the char which tptr points to, but tptr can point anywhere and you overwrite that random char that is may or may not used somewhere else, causing undefined behaviour.

What you probably want to do:

tptr=stpr;
     +--------+      +---+---+---+----+     
tptr | 0x1234 | -->  |'a'|'b'|'c'|'
tptr=stpr;
+--------+      +---+---+---+----+     
tptr | 0x1234 | -->  |'a'|'b'|'c'|'\0'|
+--------+      +---+---+---+----+   
^
+--------+        | 
tptr | 0x1234 | -------+
+--------+    
'| +--------+ +---+---+---+----+ ^ +--------+ | tptr | 0x1234 | -------+ +--------+

This sets the pointer tptr to the same address as stpr points to, i.e. the string "abc".

huangapple
  • 本文由 发表于 2023年7月17日 22:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定