Thread: Character pointer and integer pointer

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    Character pointer and integer pointer

    Hello everyone
    Here there are two codes one of them work and in others it doesn't work
    Can tell me why? (I know the rule, but I want to know the reason)
    Code:
    char *ChrP="Hello";
    int  *IntP={1,2,3};//why we cannot use integer pointer like character
    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You've run into a historical anomaly in the language.

    Originally, in early versions of C, it was not possible to initialise any pointer using an array initialiser, with any type (a string literal can be used a special type of initialiser for arrays of chars). A number of vocal programmers then got tired of doing
    Code:
        char s[] = "Hello";
        char *p = s;
           /*  use p */
    and wanted to do this
    Code:
        char *p = "Hello";
    and lobbied to get that supported by some compilers. The vendors of the respective compilers then successfully lobbied to get it into the first C standard. And now we're stuck with it.

    Some compilers do support such abominations (yes, that's what I think such constructs are) for other types.

    Most modern compilers (whether they support such things for non-char types or not) will give warnings, if you turn up the warning levels. It is also considered good practice (at least, for C99 and later) for definition of CharP to have a const qualifier. Even then, it is an anomaly, even if a little less abominable.

    There was some lobbying to get the anomaly supported by the standard for types other than char. I'm not sure if that got past the standards committee though, so don't know if that feature made it into C11. I hope it didn't.

    My personal opinion is that both lines of your sample should be invalid (i.e. compilers should complain bitterly about both, and reject such code out of hand). The reason I say that is that programmers tend to misuse the "feature", and find themselves accidentally modifying string literals via a pointer (which is undefined behaviour) when they pass it to a function. I remember reading a report which claimed the number of errors in real-world code resulting from that construct is huge, but never confirmed veracity of that report. I have certainly seen programmers regularly make errors like that though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-07-2013, 10:53 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM
  5. pointer variable to integer?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 05:06 PM

Tags for this Thread