0

why does the compiler act in a different way when a string is initialized or declared than it does in case of an integer pointer ?

i mean we do that

char *x = "cat"

not

char* x = &(of something )

like an int

int* x = &(of variable )

why do we input the values directly in case of char* x not the address ?

1

It's not as different as you think.

Specifying the string literal "cat" will cause the compiler to place four bytes {'c', 'a', 't', '\0'} in a read-only area, and replace the literal with a pointer to the first of those bytes.

As a string in C is the pointer to the first character (regardless of where the characters are stored), there is no further indirection.

  • the literal itself returns a pointer so it’s like cha* = other pointer right ? – Antwan Samir Mar 31 '19 at 11:24
  • Correct, exactly like when you apply the & operator to a variable. – Blauelf Mar 31 '19 at 11:27
  • Got it , really appreciate your help – Antwan Samir Mar 31 '19 at 11:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .