Thread: uhg pointers and passing thru funcion prams.

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Oh. Hmm, I think I see what you mean now. I wonder why you care when you have perfectly good strings to pass to your int_or_ch() function in findX().

    Think of it this way.
    Code:
     void dbl(int x)
    { 
       x *= 2;
    }
    In order to make the change to x persist beyond the call to dbl(x); we have to change the type of the argument to int *x and, accordingly, it becomes dbl(&x);

    If you want to change an int pointer, the same logic applies. The argument becomes int **x; because what we really want to change is a memory address, not the value.

    The same logic applies to char * type. You don't want to change a string, you want to change a pointer from main(), so you have to use char **. But that makes your function this:
    Code:
    void findXChar (char *input, char **width, char **height) 
    {
       *width = strtok_r(input, "x", height);
    }
    But I don't think you really need this function, you will just be using your int_or_ch() function later.
    Last edited by whiteflags; 10-21-2017 at 02:17 PM.

  2. #17
    Banned
    Join Date
    Aug 2017
    Posts
    861
    oH MY youzzzz
    it is still doing that problem I was having
    Code:
    userx@slackwhere:~/bin/beta
    $ ./a.out 333x888 // <-- proper input gives proper results 
    Output width: 333 by height: 888 // <- here
    userx@slackwhere:~/bin/beta
    $ ./a.out 333x8d3 // improper input gives improper results 
    Output width: 333 by height: 8 // <--- here
    it does not matter how many times strtok is called apparently your code is still not stopping me from getting bad data due to improperly formatted input.

    it will instead try to render an image 333x8
    which it can do but that is not the suggested size the user intended. because of a letter mistakenly added to the equation.

    whereas my code I submitted does that, it will not allow it and it has nothing to do with how many times strtok is called,

    the real problem here is it is just not returning the char* in the prams which is the real problem.



    with that code off the top of my head I got to check length twice , once before with x and then do the math on the other side -1 for x if it does not work out in the math then throw an error
    Last edited by userxbw; 10-21-2017 at 02:33 PM.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by userxbw View Post
    oH MY youzzzz
    Could you be less obnoxious please.
    it is still doing that problem I was having
    Code:
    userx@slackwhere:~/bin/beta
    $ ./a.out 333x888 // <-- proper input gives proper results 
    Output width: 333 by height: 888 // <- here
    userx@slackwhere:~/bin/beta
    $ ./a.out 333x8d3 // improper input gives improper results 
    Output width: 333 by height: 8 // <--- here
    it does not matter how many times strtok is called apparently your code is still not stopping me from getting bad data due to improperly formatted input.
    If your int_or_ch() function implementation works, you can use it and this problem goes away.

    Any problems with the numbers are problems from my implementation of int_or_ch(). I only wrote the one that I posted because I didn't want to use something else.

    You deciding that this is related to the whole idea is a bit unfortunate, as I routinely demonstrate, calling strtok_r() more than once isn't really necessary.

    You deciding what's important all the time, might be why it is so hard for you to find decent help here.

    the real problem here is it is just not returning the char* in the prams which is the real problem.

    Yes, that is pretty true, but I addressed that earlier.

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    still will not work due to red comments in code
    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    
    int int_or_ch(const char *str, unsigned int *result)
    {
            return sscanf(str, "%u", result) == 1;
    }
    
    int main(int argc, char *argv[])
    {
    
            if (argc < 2)
            {
                    puts("example usage: prog 255x255");
                    return 0;
            }
    
            int len1 = strlen(argv[1]);
    
            // argv is modifiable. as much as i would prefer to modify a copy,
            // we'll do it on the original.
            char *tokenAfterX;
            char *tokenBeforeX = strtok_r(argv[1], "x", &tokenAfterX);
    
            if (tokenAfterX == NULL)
            {
                    puts("Error on input. x expected.");
                    puts("example usage: prog 255x255");
                    return 0;
            }
    
            unsigned int width, height;
    
            int len2 = strlen(tokenBeforeX);
            int len3 = strlen(tokenAfterX);
    
            // remove 1 for x
            if ( len1 - 1 != (len2+len3))
                printf("Not equal %d : %d\n", len1 - 1, (len2+len3) );
            // this does not work because it returns that same as my FindX does
            // using two tok holders
            // the only thing elimnatied was calling it twice
            // it still returns whatever is on either side of x
            // then gets sent to be chaned into a int,
            // the only difference is that it will not go past
            // the letter, then use what ever digit is before
            // the letter
            //
    
            // it does not prevent the using of the badly
            // formated data as a whole, ie 3f4.
    
    
            // I'm assuming width is first
            if (!int_or_ch(tokenBeforeX, &width) || !int_or_ch(tokenAfterX, &height))
            {
                    puts("Error in input. Expected integers.");
                    return 0;
            }
    
            printf("Output width: %u by height: %u\n", width, height);
            return 0;
    }
    
    
    /*
    int int_or_ch(const char *str, unsigned int *result)
    {
            return sscanf(str, "%u", result) == 1;
    }
    
    int findX(char *c, char *returned )
    {
    char *tokenAfterX;
    char *tokenBeforeX = strtok_r(c, "x", &tokenAfterX);
    
    
    
            if (tokenAfterX == NULL)
            {
                    puts("Error on input. x expected.");
                    puts("example usage: prog 255x255");
                    return 0;
            }
    
            returned = tokenAfterX;
            printf(" tokenBeforeX %s tokenAfterX %s\n", tokenBeforeX, tokenAfterX);
    return 0;
    }
    int main(int argc, char *argv[])
    {
    
    char *what_did_I_get = "\0";
    
            if (argc < 2)
            {
                    puts("example usage: prog 255x255");
                    return 0;
            }
    
            // argv is modifiable. as much as i would prefer to modify a copy,
            // we'll do it on the original.
    
            findX(argv[1], what_did_I_get);
    
            printf("what_did_I_get %s\n", what_did_I_get );
    
            unsigned int width, height;
    
            // I'm assuming width is first
            if (!int_or_ch(tokenBeforeX, &width) || !int_or_ch(tokenAfterX, &height))
            {
                    puts("Error in input. Expected integers.");
                    return 0;
            }
    
            printf("Output width: %u by height: %u\n", width, height);
    
            return 0;
    }
    **/

  5. #20
    Registered User
    Join Date
    May 2015
    Posts
    90
    If I input 333x3fd

    Is tokenBeforeX = "333" & tokenAfterX = "3fd"?

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Fiskker View Post
    If I input 333x3fd

    Is tokenBeforeX = "333" & tokenAfterX = "3fd"?
    exactly,what she was saying is my using strtok twice was my problem, my findX function does the same as hers does,
    then it needs to get checked for isdigit for all three on both sides, if that fails then catch it,
    both my function does that first operation of getting the sides of the x , my other function needs that to be a char* so it can check it to make sure that is does not have any letters in it, if yes throw error, catch it stop program from going any further, The problem I am having is passing char pointers to get it returned a pointer so I can pass it into my other function that does what I need. post #8

    this is s two step operation. step 1 get what ever is one either side of x step 2 then check it to make sure it is only digits, if true send to another function as int, if false stop program.



    step one I cannot figure out how to pass a char back to get a char* to go to step 2.

    her second function just takes that 3fd and sends back 3 as an int . it fails
    Last edited by userxbw; 10-21-2017 at 07:31 PM.

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861

    this is ran in C in this example

    this was all I needed to pass back through the prams the results.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int findXChar(char *whereisX, const char **wantW, const char **wantH)
    { //std::cout<<"in findxchar"<< whereisX<<std::endl;
    /*        char *tokenAfterX;
            char *tokenBeforeX = strtok_r(data, "x", &tokenAfterX);
            *width = tokenBeforeX;
            *height = tokenAfterX;
    // does not work for both sides still returns if no X was added to format
    // 666666 still gets a return of 666666 for w and h gets 0
    // that is why it HAS to be done this way
        * */
    
        char *tok1, *tok2, *saveptr;
        //char str1[1+strlen(whereisX)];
        //strcpy(str1, whereisX);
        // changed to strdup
        char *dup = strdup(whereisX);
        const char ch = 'x';
        if( strrchr(whereisX, ch ) == NULL)
        {
            return 1;
        }
        else
        {
    
       tok1 = strtok_r(whereisX, "x", &saveptr);
       tok2 = strtok_r(NULL, "x", &saveptr);
    
        if ( tok2 == NULL)
        {
            return 1;
        }
         else
            {
               tok1 = strtok_r(dup, "x", &saveptr);
               tok2 = strtok_r(NULL, "x", &saveptr);
               *wantW = tok1;
               *wantH = tok2;
               return 0;
             }
        }
    
    } //end findX
    int int_or_ch(const char *data)
    {
        int len = 0, i;
        char *endptr;
    
        len = strlen(data);
    
        for ( i = 0; i < len; i++)
        {// if not a digit return error code -1
            if ( isdigit(data[i]) == 0)
                return -1;
         }
         return strtol(data, &endptr, 10);
    }
    
    int main (int argc, char **argv)
    {
        const char *x,*y;
        findXChar(argv[1], &x,&y);
        printf("as string/char x %s y %s\n", x,y);
        int got_x, got_y;
    
        if ( (got_x = int_or_ch(x)) == -1 )
            printf("got -1 bad format\n");
    
        if ( ( got_y = int_or_ch(y) ) == -1 )
            printf("got -1 bad format\n");
    
        printf("as int x %d y %d\n", got_x,got_y);
    return 0;
    }
    Code:
    userx@slackwhere:~/bin/testfindx
    $ ./main_c 44dx885
    as string/char x 44d y 885
    got -1 bad format
    as int x -1 y 885
    userx@slackwhere:~/bin/testfindx
    $ ./main_c 666x897
    as string/char x 666 y 897
    as int x 666 y 897
    it was the char * to const char * that needed to be changed to get me what I was really wanting, asked for. thanks! to all that helped. Smith in C++ side too.
    Last edited by userxbw; 10-22-2017 at 03:57 PM.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Only showing what is possible.
    Code:
    #include <stdio.h>
    int main(int argc, char *argv[]) 
    {
       if (argc == 2) {
          unsigned int width = 0;
          unsigned int height = 0;
          int ret = 0;
          char trail = '\0';
    
          ret = sscanf(argv[1], "-c=%ux%u%c", &width, &height, &trail);
          if (ret == 2 && trail == '\0')
             printf("Output width: %u by height: %u\n", width, height);
          else 
             printf("Bad format. Received \"%s\"\n", argv[1]);
       }
       else {
          printf("Example usage: prog -c=255x255\n");
       }
       return 0;
    }
    
    C:\Users\jk\Desktop>sandbox
    Example usage: prog -c=255x255
    
    C:\Users\jk\Desktop>sandbox 255x255
    Bad format. Received "255x255"
    
    C:\Users\jk\Desktop>sandbox -c=300x84e
    Bad format. Received "-c=300x84e"
    
    C:\Users\jk\Desktop>sandbox -c=300x8d4
    Bad format. Received "-c=300x8d4"
    
    C:\Users\jk\Desktop>sandbox -c=9q9rxafujf
    Bad format. Received "-c=9q9rxafujf"
    
    C:\Users\jk\Desktop>sandbox -c=300x400
    Output width: 300 by height: 400

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    Only showing what is possible.
    Code:
    #include <stdio.h>
    int main(int argc, char *argv[]) 
    {
       if (argc == 2) {
          unsigned int width = 0;
          unsigned int height = 0;
          int ret = 0;
          char trail = '\0';
    
          ret = sscanf(argv[1], "-c=%ux%u%c", &width, &height, &trail);
          if (ret == 2 && trail == '\0')
             printf("Output width: %u by height: %u\n", width, height);
          else 
             printf("Bad format. Received \"%s\"\n", argv[1]);
       }
       else {
          printf("Example usage: prog -c=255x255\n");
       }
       return 0;
    }
    
    C:\Users\jk\Desktop>sandbox
    Example usage: prog -c=255x255
    
    C:\Users\jk\Desktop>sandbox 255x255
    Bad format. Received "255x255"
    
    C:\Users\jk\Desktop>sandbox -c=300x84e
    Bad format. Received "-c=300x84e"
    
    C:\Users\jk\Desktop>sandbox -c=300x8d4
    Bad format. Received "-c=300x8d4"
    
    C:\Users\jk\Desktop>sandbox -c=9q9rxafujf
    Bad format. Received "-c=9q9rxafujf"
    
    C:\Users\jk\Desktop>sandbox -c=300x400
    Output width: 300 by height: 400
    thank you I highly appreciate it, I'll check it out in the morning when I get more time.

  10. #25
    Banned
    Join Date
    Aug 2017
    Posts
    861

    holy zero's Batman looks like ...

    Quote Originally Posted by whiteflags View Post
    Only showing what is possible.
    Code:
    #include <stdio.h>
    int main(int argc, char *argv[]) 
    {
       if (argc == 2) {
          unsigned int width = 0;
          unsigned int height = 0;
          int ret = 0;
          char trail = '\0';
    
          ret = sscanf(argv[1], "-c=%ux%u%c", &width, &height, &trail);
          if (ret == 2 && trail == '\0')
             printf("Output width: %u by height: %u\n", width, height);
          else 
             printf("Bad format. Received \"%s\"\n", argv[1]);
       }
       else {
          printf("Example usage: prog -c=255x255\n");
       }
       return 0;
    }
    
    C:\Users\jk\Desktop>sandbox
    Example usage: prog -c=255x255
    
    C:\Users\jk\Desktop>sandbox 255x255
    Bad format. Received "255x255"
    
    C:\Users\jk\Desktop>sandbox -c=300x84e
    Bad format. Received "-c=300x84e"
    
    C:\Users\jk\Desktop>sandbox -c=300x8d4
    Bad format. Received "-c=300x8d4"
    
    C:\Users\jk\Desktop>sandbox -c=9q9rxafujf
    Bad format. Received "-c=9q9rxafujf"
    
    C:\Users\jk\Desktop>sandbox -c=300x400
    Output width: 300 by height: 400
    Code:
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <sstream>
    #include "data.h"
    
    using namespace std;
    
    int gettingWidthHeight(char *data, unsigned int *w, unsigned int *h)
    {
    
        unsigned int width = 0, height = 0;
        int ret = 0;
        char trail = '\0';
    
        ret = sscanf(data,"%dx%d%c", &width,&height,&trail);
        if (ret == 2 && trail == '\0')
        {
            *w = width;
            *h = height;
            return 0;
        }
        else
            return -1;
    }
    
    int main(int argc, char **argv)
    {
    
        if (argc < 2)
        {
            cout<<"Nope, needs args noodle head"<<endl;
            cout<<"usage 333x123"<<endl;
            exit(1);
        }
        unsigned int w,h;
        char *sendingChar = strdup(argv[1]);
        if ( gettingWidthHeight(sendingChar,&w,&h) == -1) {
            cout<<"Badly formed: "<<argv[1]<<endl;
            exit(1);}
    
        cout<<"width "<<w<<" height "<<h<<endl;
        free(sendingChar);
    
    return 0;
    }
    specs:
    (optional) integer number (greater than zero) that specifies maximum field width, that is, the maximum number of characters that the function is allowed to consume when doing the conversion specified by the current conversion specification. Note that %s and %[ may lead to buffer overflow if the width is not provided.
    I see how it works then I read that so I tried that and
    Code:
    userx@slackwhere:~/bin/testfindx
    $ ./main 3e2x4es
    Badly formed: 3e2x4es // works
    userx@slackwhere:~/bin/testfindx
    $ ./main 3e2x4es
    Badly formed: 3e2x4es //works
    userx@slackwhere:~/bin/testfindx
    $ ./main 333x333
    width 333 height 333 //works
    userx@slackwhere:~/bin/testfindx
    $ ./main 333x333f
    Badly formed: 333x333f //works because of trail = '\0' after 2nd int
    
    // zero's are working 
    
    userx@slackwhere:~/bin/testfindx
    $ ./main 303x330
    width 303 height 330
    userx@slackwhere:~/bin/testfindx
    $ ./main 303x300
    width 303 height 300
    userx@slackwhere:~/bin/testfindx
    $ ./main 003x300
    width 3 height 300 // <-- bam! user error, still needs a second check?
    The function works, thanks, but due to me and what if's - I still need to write me a different function to check for leading zero's to cover that, what if the user does that? to cover the just in case scenario.
    or I could just leave it to user error, with no check, then they'd have to use their own brain to figure out why they got what they got if the image does not display as they may have thought is was going to.

    thanks that is a big change. Now I walk away with knowing how to pass char * through the prams of a function, and have been introduced further to the uses of how to use sscanf cool! who wants a banana? I'm out of cookies.
    Last edited by userxbw; 10-23-2017 at 07:54 AM.

  11. #26
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The answer really depends on why you think this is an error. If it's because the user padded the 3 with leading zeros, note that you can do this anytime you enter an integer. The input should be accepted because padding with leading zeros does not change a value 0123 means zero thousands, 1 hundreds, 2 tens, and 3 ones, see. If it is an error because the dimension is too small, then yes, make additional checks.

    I now think that there is some minimum dimension that you will accept, but I don't know what. Whatever it is, just make sure that width and height are at least than the minimum.
    Code:
          ret = sscanf(argv[1], "-c=%ux%u%c", &width, &height, &trail);
          if (ret == 2 && trail == '\0' && width >= MIN_WIDTH && height >= MIN_HEIGHT)
             printf("Output width: %u by height: %u\n", width, height);
          else
             printf("Bad format. Received \"%s\"\n", argv[1]);

  12. #27
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    The answer really depends on why you think this is an error. If it's because the user padded the 3 with leading zeros, note that you can do this anytime you enter an integer. The input should be accepted because padding with leading zeros does not change a value 0123 means zero thousands, 1 hundreds, 2 tens, and 3 ones, see. If it is an error because the dimension is too small, then yes, make additional checks.

    I now think that there is some minimum dimension that you will accept, but I don't know what. Whatever it is, just make sure that width and height are at least than the minimum.
    Code:
          ret = sscanf(argv[1], "-c=%ux%u%c", &width, &height, &trail);
          if (ret == 2 && trail == '\0' && width >= MIN_WIDTH && height >= MIN_HEIGHT)
             printf("Output width: %u by height: %u\n", width, height);
          else
             printf("Bad format. Received \"%s\"\n", argv[1]);
    it is what qualifies as a valid width and length size of an image,
    and trying apply precognitive thinking. did the user really want
    003x150 as a width and length? whereas the leading zeros just
    cancel out. making it 3x150 no error, or did the user just experience
    some type of temporary dyslexia by typing it backwards?

    because Imlib2 can render it down to a 1x1 sized image, it is just
    dots if tiled across the screen, but it is still there.

    Code:
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=250x130 -n 540 /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=250x1f0 -n 540 /media/data/wallpaper
    Badly 250x1f0 formatted size
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=1x1 -n 540 /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=2x2 -n 540 /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t hv=2x2 -n 540 /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t v=120x450  -n 1000  /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=1x1  -n 1000  /media/data/wallpaper
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -t n=1x1  -n 10000  /media/data/wallpaper
    Image number 10000 out of range.
    
    userx@slackwhere:/media/data/mh3000-beta
    $ mh3000-beta -c=1x1  -n 345  /media/data/wallpaper
    userx@slackwhere:~
    $ mh3000-beta -c=350x1 -s "#00ffbb" -n 596 /media/data/wallpaper
    //same as
    userx@slackwhere:~
    $ mh3000-beta -c=350x001 -s "#00ffbb" -n 596 /media/data/wallpaper
    so it is not really an issue unless I make it one. thanks for your input.
    Last edited by userxbw; 10-23-2017 at 11:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coloring Graph need help with funcion
    By Mateusz Labuda in forum C Programming
    Replies: 3
    Last Post: 05-30-2016, 11:48 AM
  2. Would this be considered defining a funcion?
    By 777funk in forum C Programming
    Replies: 6
    Last Post: 12-13-2010, 09:17 AM
  3. VC++ funcion call problem
    By TriKri in forum Windows Programming
    Replies: 1
    Last Post: 01-11-2010, 04:59 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM

Tags for this Thread