Thread: Another sscanf() question.

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Another sscanf() question.

    Code:
    #include <stdio.h>
    
    void valid_buy(int *, char *, float *);
    
    int main()
    {
        int quantity = 0;
        char description[80];
        float price = 0;
    
        valid_buy(&quantity, description, &price);
    
        printf("Quantity: %d\nDescription: %s\nPrice: %f\n", quantity, description, price);
    
        return 0;
    }
    
    void valid_buy(int *quantity, char *description, float *price)
    {
        char line[]= "1,blue car,2500.00";
    
        /* I know I'm ignoring the return value of sscanf() but it's just for now */
        sscanf(line, "%d, %49[^,], %f", &quantity, description, &price);
    }
    The string "blue car" gets assigned to variable description. However, int quantity and float price still have the value of 0, Why?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    void valid_buy(int *quantity, char *description, float *price)
    {
        char line[]= "1,blue car,2500.00";
    
        /* I know I'm ignoring the return value of sscanf() but it's just for now */
        sscanf(line, "%d, %49[^,], %f", &quantity, description, &price);
    }
    Note that quantity and price are already pointers. You don't need the address of the pointer. Try instead:
    Code:
    sscanf(line, "%d, %49[^,], %f", quantity, description, price);
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    quality and price are already pointers, you can safely remove the address-of operator:
    Code:
    sscanf(line, "%d, %49[^,], %f", quantity, description, price);
    My best code is written with the delete key.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Now you know why "gcc -Wall" is a good idea.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() question
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-28-2009, 02:57 PM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. sscanf question
    By Little_Dump in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 02:16 PM
  4. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 08:19 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM