Thread: My "scanf"

  1. #1
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23

    My "scanf"

    I have been writing my own function, which accepts a number (let say an integer), everything was working fine while I was using a function without variable argument list, but now I have a problem when I'm trying to put argument(which is actually an address) into a sscanf() a get an error or a program crashes.
    Code:
    int GetNum(const char *format, ...)
    {
            int i = 0;
            //only one argument for now
            int num = 1;
            int args = 0;
            char temp[30];
            char **s;
    
            va_list arguments;
            //Initializing arguments to store all values after num
            va_start(arguments, num);
            for(i = 0; format[i] != 0; i++)
            {
                    if(format[i] == '%')
                    {                      
                            i++;
                            //check if is an integer
                            if(format[i] == 'd')
                            {
                                    /*if(width > 5)
                                    {
                                            width = 5;
                                    }*/          //widht
                                    fgets(temp, 6, stdin);
                                    if(RemoveNewLine(temp) == 1)
                                    {
                                            ClrBuf();
                                    }
                                    //PROBLEM
                                    //desperet try
                                    s = &(va_arg(arguments, char *));
                                    if(sscanf(temp, "%d", s) != 1)
                                    {
                                            printf("Invalid input!\n");
                                            printf("Press Enter to continue . . .");
                                            getchar();
                                            return NULL;
                                    }
                                    args++;
                            }
                    }
            }
            va_end(arguments);
            return args;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >s = &(va_arg(arguments, char *));
    >if(sscanf(temp, "%d", s) != 1)
    Ehh, that looks all kinds of wrong. s is a pointer to pointer to char, so you ask for a pointer to char, take the address, then send it to sscanf when it expects a pointer to int? At the very least you have type mismatches.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    //Initializing arguments to store all values after num
    va_start(arguments, num);
    Erm, you're supposed to reference the last named parameter, not some random local variable.
    It might work, except you're starting off in the wrong place.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, there goes the first [machine word size] bytes eh.

    Pretty ambitious. I wouldn't be doing this sort of thing, but, well, pretty ambitious.

    I think your code would be much more useful if you got it to work in a way similiar to, say, C++ streams or something.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User marrk's Avatar
    Join Date
    Sep 2006
    Posts
    23
    smart-asses(ok I admit jafet you made me laugh with your link)

    I was trying to make a function (for noobs like me) who have problems with user inputs(board is spamed with that kind of threads).
    So solve mission was to made a function like scanf:
    -with no new line characters left in the input buffer
    -no sign of fflush()
    -limites of data types
    (-all the code written in ANSI C)

    Like I said, it works fine with "known" number of argumetns
    No offense: Prelude, Salem, jafet
    Last edited by marrk; 11-22-2006 at 05:32 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Show an example call with a variable number of arguments.
    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. Help with "scanf"
    By bearcat19 in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 11:45 AM