Simple sscanf mystery

This is a discussion on Simple sscanf mystery within the C Programming forums, part of the General Programming Boards category; Hello all, Okay I'm stumped. I must be misreading something, but I think the below program should print "1" as ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Question Simple sscanf mystery

    Hello all,
    Okay I'm stumped. I must be misreading something, but I think the below program should print "1" as the result of sscanf. That is, I believe searching for "t%c" would catch the character "h" in the below example, but I can't figure out why it always returns 0. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[], char *envp[])
    {
       /* we only care if sscanf found our substring, we don't care what it's value is */
       char *junk;
    
       junk = (char *) malloc (25);  /* allocate our memory */
    
       /* why doesn't this return 1? t%c is within "Hello there", should read 'h' */
       printf("\nsscanf returned: %i\n", sscanf("Hello there", "t%c", junk));
    
       free (junk); /* free up our memory */
    
       return (0); /* always return success */
    
    } /* end main(...) */
    I hate just flat-out asking for the answer but it seems I fundamentally misunderstand either the syntax or the sscanf function itself. Thanks for any hints or pointers.

    Josh

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char junk;
       /* You may want to tell sscanf to skip the stuff that's not a 't'. */
       printf("\nsscanf returned: %i\n", sscanf("Hello there", "%*[^t]t%c", &junk));
       return (0);
    
    }
    
    /* my output
    sscanf returned: 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    Thanks!

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    41
    Just one more quick question. Why does junk[2] remain -1 in the below code? Shouldn't junk[2] contain '3456'?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[], char *envp[])
    {
       int *junk;
       char *data;
       int  i, value;
    
       data = (char *) malloc (15);
       junk = (int *) malloc (15);
    
       for (i = 0; i < 15; i++)
       {
          junk[i] = -1;
       }
    
       strcpy(data, "09+234.88\r\n A151 L+3000 C3456\r\n");
    
       value = sscanf(data, "%*[^ ] A%i L+%i C%i\r\n", &junk[0], &junk[1], &junk[2]);
    
       for (i = 0; i < 5; i++)
       {
          printf("\njunk[%i]: %i", i, junk[i]);
       }
    
       printf("\nvalue: %i\n\n", value);
    
       free (data);
    //   free (junk); SIGSEV :(
    
       return (0); /* always return success */
    
    } /* end main(...) */

    My output is:

    junk[0]: 151
    junk[1]: 3000
    junk[2]: -1
    junk[3]: -1
    junk[4]: -1
    value: 2

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    data = (char *) malloc (15);
    junk = (int *) malloc (15);
    You are not allocating 15 integers. You are allocating 15 bytes.

    Try this on for size:

    junk = malloc( sizeof( int ) * 15 );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21