Thread: Is this valid

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Is this valid

    Code:
    char str[] = "\"-d\"";
     
    sscanf(str, "\"%[^\"]", str);
    I am not pretty sure, whether this is legal or not. But it seems to compile fine with no error. The thing which i am worried about here is that, I am using the same char array "str" to point at fetched data again in sscanf. Is it right?

    Thank you

    sshairsh

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I am using the same char array "str" to point at fetched data again in sscanf. Is it right?
    Yes, you're right to be worried, because it's wrong.

    Very few of the C standard library functions tolerate overlapping input and output data (memmove is one example which does). For pretty much everything else, assume undefined behaviour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Salem View Post
    Very few of the C standard library functions tolerate overlapping input and output data (memmove is one example which does). For pretty much everything else, assume undefined behaviour.
    Salem, thanks a lot for that. I dint really knew about it. But I had a half mind saying that it wan't right. But now its all clear.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum valid address value
    By BMintern in forum C Programming
    Replies: 6
    Last Post: 04-12-2008, 08:21 AM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Are these valid operators
    By Morgan in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 11:56 AM
  4. valid test expression?
    By Draco in forum C Programming
    Replies: 11
    Last Post: 08-07-2003, 05:25 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM