How does char * blah = hello work?

When you create a string from pointers char, how does it work?

char *name = "ben";

Is this a "hidden" pointer arithmetic?

+3
source share
7 answers

There is no hidden pointer arithmetic, but I suspect you want a more detailed answer.

If you have a function:

void foo() {
    char * bar = "Hello World";
}

There are actually two pieces of memory:

  • -, 12 "Hello World" (1 NULL ). . ( ) ( segfault).
  • - , bar. foo(), (4 32 ) . , foo().

, :

bar = "Good bye";

"Hello World" "Good bye". 3- "Good bye" ( ), (bar) , .


"" ​​( ) :

void foo() {
    char bar[] = "Hello World";
}

, (, ). , , , ( "Hello World" + ), .

, gcc -S test.c, test.s.


- C.

, , , , , , ( ).

+6

. , .

, , "ben" ( .rodata ELF), name .

+8

"" ?

. . , *. .

+4

C - , , n '\ 0' . char * p = "string", p.

, , "ben" "b", "e", "n" "\ 0" . , . . 'name' , , , .

+4

- , :

char name[] = "ben";
0

name - , "b" ( "h" ). , . .

0

​​, , , ( ). , , , . , , "const", . , , seg.

0

Source: https://habr.com/ru/post/1752566/


All Articles