2

char *p = "a"; is valid but not int *p = 2; and char *p = 'a'; Why are they designed like that?

Karthik
  • 365
  • 1
  • 3
  • 16
  • What's the necessity of `while` and `do...while`? Shouldn't `for` be enough? Technically yes, but having all three gives options to express yourself more clearly. – Jon Jul 02 '13 at 15:30
  • 1) "a" has some constant address in memory but 2 not. 2)To increase something **before** or **after**. – huseyin tugrul buyukisik Jul 02 '13 at 15:30
  • 2
    "a" is a `char[]` which can be converted to `char *`. – user1937198 Jul 02 '13 at 15:31
  • @huseyintugrulbuyukisik, beofre or after what? ;) – StoryTeller - Unslander Monica Jul 02 '13 at 15:31
  • `char *` wants to be assigned a pointer to a character, `'a'` is just a character so you can't assign that. `"a"` is a pointer to a character, so that's OK to assign. `int *` wants a pointer to an integer, `2` is just an integer, not a pointer. – Mike Jul 02 '13 at 15:32
  • @StoryTeller errr. things going on, compiled things, assembly lines, branching. – huseyin tugrul buyukisik Jul 02 '13 at 15:32
  • 2
    It is perfectly clear what is asked in both cases. That is no reason to close this post. Vote to reopen. – Lundin Jul 02 '13 at 15:32
  • 2
    Let's remove the second question and just deal with the first, which is legitimate, clear, and answerable. – Chris Stratton Jul 02 '13 at 15:32
  • for(int i=0;(i++)<20;){} how does this work for example? – huseyin tugrul buyukisik Jul 02 '13 at 15:33
  • @ChrisStratton The second question is perfectly fine. And the answer is: because the C language isn't sane and logical. Though since they are not related, they should have been posted as 2 different questions – Lundin Jul 02 '13 at 15:34
  • @huseyintugrulbuyukisik, the real need is syntactic sugar. Both operators return the variables value. But one returns the old value, and the second returns the new value. – StoryTeller - Unslander Monica Jul 02 '13 at 15:34
  • The ++ operators should never be used together with most other operators anyhow, since it easily leads to poorly-defined behavior or unreadable code. And combining any ++ with other operators will yield no better machine code, just more unreadable C code. – Lundin Jul 02 '13 at 15:36
  • 1
    @Lundin, IDK. Some idioms involving the increment operators are pretty self explanatory to any moderately experienced programmer. Consider `while(*p++)` for instance. – StoryTeller - Unslander Monica Jul 02 '13 at 15:43
  • 1
    The *original* rationale for having both pre- and post-increment and decrement probably had something to do with the [PDP-11/20's set of addressing modes](https://en.wikipedia.org/wiki/PDP-11_architecture#General_register_addressing_modes). Note in particular that it has predecrement and postincrement, but not the other two. – zwol Jul 02 '13 at 15:51
  • I am adding second as a separate question. – Karthik Jul 02 '13 at 16:35
  • @StoryTeller The problem is that it is not intuitive to anyone else. Experienced C programmers know all the pitfalls of the language. Given enough time, any weird crap will seem natural. Also, having double sets of ++ operators is completely superfluous. – Lundin Jul 02 '13 at 18:18

3 Answers3

4

Anything wrritten inside " " is considered as string in C.So

char *p = " a" says you are passing base address of string to char pointer.which is Valid.

int *p says p is a pointer to integer so p can hold a address to integer so int *p = 2 is not valid.

Similarly char *p is pointer to character so p can hold address of a character so char *p = 'a' is not valid because 'a' is just a character not address to character.

Dayal rai
  • 6,548
  • 22
  • 29
3

A typeof string literal is of char[n] and assignment to char* is fine.
Point is both are pointers.

char *p = "a"; means p points to string "a" (some where in memory, type of "a" is char[2]).

   p            23   24
  +----+      +----+----+
  | 23 |      | a  | \0 |        
  +----+      +----+----+

Whereas 2 and 'a' are of int type values, not a valid address hence following declarations are errors/warnings: "initialization makes pointer from integer without a cast"

int *p = 2; and   
char *p = 'a';

note: in a char constant is int type, but not char refrence.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Actually a string literal has type `char*`, for historical reasons, even though you shouldn't try to modify its contents. – Fred Foo Jul 02 '13 at 15:49
  • Strictly speaking, a string literal is an array, not a pointer. It does (like other arrays) decay into a pointer to its first element in most contexts. – Carl Norum Jul 02 '13 at 15:49
  • @CarlNorum you means `char[]` not `const char*` ? – Grijesh Chauhan Jul 02 '13 at 15:51
  • 1
    Strictly speaking in C the type of string literal is `char [N]`. It is an array, not a pointer, but there's no `const` in the type. – AnT stands with Russia Jul 02 '13 at 15:55
  • 2
    @Grijesh with gcc you can force the compiler to treat string litterals as 'const char *'. The option -Wwrite-strings will do that. While it is not standard C, it is really useful to make the code more robust. – Patrick Schlüter Jul 02 '13 at 16:15
  • @tristopia I was unaware of this thanks tristopia, I just posted an answer because no one else was interested But I learnt. Thanks :) – Grijesh Chauhan Jul 02 '13 at 16:19
1

Because "a" has a type of char *, and 2 doesn't have a type of int *. Note that char *p = 'a' is also invalid.

hobbs
  • 223,387
  • 19
  • 210
  • 288