Thread: char

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    char

    hello friends
    Code:
    int main()
    {
    char*p;
    p="23";
    printf("%c",p[0]+p[1]);
    }
    the result is not 5 why this?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Because you never allocated memory for your pointer, and you can't use the assignment operator after the initial definition when dealing with C-string, you would need to use the strcpy() function.

    Jim

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I think jim's been drinking!

    "23" is shorthand for the char array
    Code:
    {'2','3','\0'}
    '2' is a number that is dependent on the character set of the system. In ascii, it is the number 50 (decimal).

    '3' in ascii is 51.

    50 + 51 is 101, which in ascii is the character 'e'.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  3. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM