Thread: Confused with Sprintf

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Confused with Sprintf

    Ok this might seem kinda a small problem but why can't I do this?

    char *temp="Hello";
    char *temp2;
    sprintf(temp2, "%sWorld, temp);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Because temp2 is a pointer to char and doesn't have any space allocated for storage. Try char temp2[11]; instead.

    You get the 11 from the length of temp plus the length of "World" plus 1 for the terminating 0.
    Last edited by itsme86; 09-12-2004 at 01:18 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ExtremelyStupid
    Ok this might seem kinda a small problem but why can't I do this?
    Well, you can do it, it just won't do what you want it to.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf Wrapping, a tough one
    By AdmiralKirk in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2006, 10:43 AM
  3. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM