Thread: Problems with "char*"... Please help

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Question Problems with "char*"... Please help

    I have some troubles with char * (pointer to char)...

    #1. Is the declaration

    // codes
    char* psz = "SAMPLE 1";
    // end of codes

    is equivalent to

    // codes
    char psz[] = "SAMPLE 1";
    // end of codes

    ?

    #2. And are the following codes bad ?

    // codes
    char* psz;
    psz = "SAMPLE 2";
    // end of codes

    The second codes (#2) seem to write data on out-of-services area, don't they ?

    Thanks in advance for your helps.

    Regards.

  2. #2
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    > char* psz = "SAMPLE 1";
    > char psz[] = "SAMPLE 1";

    These are coding technically the same thing. The addressing in memory is different.

    > char* psz;
    > psz = "SAMPLE 2";

    Works fine. You set the pointer psz to point to the begin of the constant c-style string.
    Making error is human, but for messing things thoroughly it takes a computer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM