Thread: character pointer question

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    character pointer question

    The following code compiles fine. How come it core dumps when I run it?

    It works when I remove the asterisk in front of the buffer variable on the 5th line. Shouldn't I always use the asterisk to reference the value the pointer points to?

    Thanks,

    Ed


    ---- code--------------------------
    #include <stdio.h>

    void report(char *buffer)
    {
    printf("Test: %s\n", *buffer);
    }

    int main(int argc, char *argv[])
    {
    char *Street = "123 Disney Way";

    report(Street);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't dereference the pointer in your printf statement. printf takes pointers, so you don't need to dereference the pointer, just pass it.

    [edit]
    Actually, let me rephrase that. printf takes arguments depending on what scan codes you give it. Since you used %s, that means it wants a pointer. If anything ever asks for a pointer, you pass it the name of a pointer as the argument, or the address of a non-pointer.
    [/edit]



    Quzah.
    Last edited by quzah; 07-10-2003 at 07:41 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    2
    k, then how about this. How come the first example works and the second crashes? Line 10 is the only variation between the two.

    Example 1:

    <code>
    #include <stdio.h>

    void update(char *buffer)
    {
    strcpy(buffer, "testing");
    }

    int main(int argc, char *argv)
    {
    char Name[20] = "Donald Duck";

    update(Name);
    }
    </code>

    Example 2:

    <code>
    #include <stdio.h>

    void update(char *buffer)
    {
    strcpy(buffer, "testing");
    }

    int main(int argc, char *argv)
    {
    char *Name = "Donald Duck";

    update(Name);
    }
    </code>

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    also, i think
    Code:
    int main(int argc, char *argv)
    should be
    Code:
    int main(int argc, char *argv[])

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  3. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM