How to compare these variables by use operator 'AND'

Discussion in 'C Programming' started by pattanawadee, Sep 22, 2003.

  1. pattanawadee

    pattanawadee Guest

    Dear all,

    I have a problem about compare the 2 variables by use operator
    'AND' while one variable is the int constant 0x00000002 and the other
    one is string '0x202'

    for the constant 0x00000002 is the flag of systemcall OPEN following
    fcntl.h

    #define O_RDWR 0x00000002 /* open for reading and
    writing */

    and the string '0x202' is the variable which I got from use command
    strtok cut string from output of ktruss such as I got
    bufft value-->open("testprg.txt", 0x202, 0x8049bb0)

    The below is my code whic use to cut string buff


    char test[80], **strsysc=NULL;
    char *sep = "()\",";
    char *word, *brkt;
    int i=0;
    int aa;

    strsysc=(char**)calloc(4,sizeof(char*));
    printf("bufft value -->%s\n",buff);
    strcpy(test,buff);
    printf("test value %s\n",test);
    for (word = strtok_r(test, sep, &brkt);
    word;
    word = strtok_r(NULL, sep, &brkt))
    {
    strsysc=word;
    printf("word==>%s\n",strsysc);
    i=i+1;
    }

    from code I got strsysc[2] = '0x202'
    and I'd like to use 'AND' to compare it

    like this if(0x00000002 & strsysc[2])
    I cannot use because strsysc[2] is string and I try to use
    atoi[strsysc[2]] the runtime error said Memory fault.

    Please give me suggestion about this case

    Regards,
    Pattanawadee
    pattanawadee, Sep 22, 2003
    #1
    1. Advertising

  2. pattanawadee

    Artie Gold Guest

    pattanawadee wrote:
    > Dear all,
    >
    > I have a problem about compare the 2 variables by use operator
    > 'AND' while one variable is the int constant 0x00000002 and the other
    > one is string '0x202'
    >
    > for the constant 0x00000002 is the flag of systemcall OPEN following
    > fcntl.h
    >
    > #define O_RDWR 0x00000002 /* open for reading and
    > writing */
    >
    > and the string '0x202' is the variable which I got from use command
    > strtok cut string from output of ktruss such as I got
    > bufft value-->open("testprg.txt", 0x202, 0x8049bb0)
    >
    > The below is my code whic use to cut string buff
    >
    >
    > char test[80], **strsysc=NULL;
    > char *sep = "()\",";
    > char *word, *brkt;
    > int i=0;
    > int aa;
    >
    > strsysc=(char**)calloc(4,sizeof(char*));
    > printf("bufft value -->%s\n",buff);
    > strcpy(test,buff);
    > printf("test value %s\n",test);
    > for (word = strtok_r(test, sep, &brkt);
    > word;
    > word = strtok_r(NULL, sep, &brkt))
    > {
    > strsysc=word;
    > printf("word==>%s\n",strsysc);
    > i=i+1;
    > }
    >
    > from code I got strsysc[2] = '0x202'
    > and I'd like to use 'AND' to compare it
    >
    > like this if(0x00000002 & strsysc[2])
    > I cannot use because strsysc[2] is string and I try to use
    > atoi[strsysc[2]] the runtime error said Memory fault.
    >
    > Please give me suggestion about this case
    >

    Use `strtol' instead. It will deal with numbers rendered in hex
    properly.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Artie Gold, Sep 22, 2003
    #2
    1. Advertising

  3. On 21 Sep 2003 21:41:23 -0700,
    (pattanawadee) wrote:

    >Dear all,
    >
    > I have a problem about compare the 2 variables by use operator
    >'AND' while one variable is the int constant 0x00000002 and the other
    >one is string '0x202'


    There is no AND operator in C. There are, however, two operators
    which have the word "and" in their titles. It is a shame you don't
    tell us which one you mean until some 40 lines later.

    >
    >for the constant 0x00000002 is the flag of systemcall OPEN following
    >fcntl.h
    >
    >#define O_RDWR 0x00000002 /* open for reading and
    >writing */
    >
    >and the string '0x202' is the variable which I got from use command
    >strtok cut string from output of ktruss such as I got
    >bufft value-->open("testprg.txt", 0x202, 0x8049bb0)
    >
    >The below is my code whic use to cut string buff
    >
    >
    > char test[80], **strsysc=NULL;
    > char *sep = "()\",";
    > char *word, *brkt;
    > int i=0;
    > int aa;
    >
    > strsysc=(char**)calloc(4,sizeof(char*));


    You did not include stdlib.h which causes the call to calloc to invoke
    undefined behavior. If you hadn't included the unnecessary and
    undesirable cast, the compiler would have issued a diagnostic warning
    you about this.

    > printf("bufft value -->%s\n",buff);


    Give us a clue, what is buff?

    > strcpy(test,buff);
    > printf("test value %s\n",test);
    > for (word = strtok_r(test, sep, &brkt);


    Where is the prototype for strtok_r? What is its return type.
    > word;
    > word = strtok_r(NULL, sep, &brkt))
    > {
    > strsysc=word;
    > printf("word==>%s\n",strsysc);
    > i=i+1;
    > }
    >
    > from code I got strsysc[2] = '0x202'


    Did you mean "0x202"? If the string actually starts with 0x, atoi
    will always return a 0 value since x is not a decimal digit.

    >and I'd like to use 'AND' to compare it
    >
    >like this if(0x00000002 & strsysc[2])


    & is the bit-wise and operator. It will not tell you if the two
    values are equal. It will only tell you if their binary
    representations have at least one bit in common on or not.

    >I cannot use because strsysc[2] is string and I try to use
    >atoi[strsysc[2]] the runtime error said Memory fault.


    Did you mean atoi(strsysc[2])? The statement
    if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
    should test the bits of the value represented by strsysc[2].
    >
    >Please give me suggestion about this case
    >
    >Regards,
    >Pattanawadee




    <<Remove the del for email>>
    Barry Schwarz, Sep 22, 2003
    #3
  4. pattanawadee

    pattanawadee Guest

    Barry Schwarz <> wrote in message news:<bkm4vp$sf6$3@216.39.135.208>...
    > On 21 Sep 2003 21:41:23 -0700,
    > (pattanawadee) wrote:
    >


    >
    > You did not include stdlib.h which causes the call to calloc to invoke
    > undefined behavior. If you hadn't included the unnecessary and
    > undesirable cast, the compiler would have issued a diagnostic warning
    > you about this.


    sorry !! I cut only some of my code I alredy include stdlib.h.
    >
    > > printf("bufft value -->%s\n",buff);

    >
    > Give us a clue, what is buff?


    bufft value-->open("testprg.txt", 0x202, 0x8049bb0)
    if up to what the systemcall is used in this case is case I want to
    catch the flag of system call OPEN from buff value it is '0x202'


    > Did you mean atoi(strsysc[2])? The statement
    > if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
    > should test the bits of the value represented by strsysc[2].


    I try test this line follow your suggestion but it still tell me
    on runtime error that 'Memory Falut'


    What I am doing is I try to modified the ktruss command (file dump.c)
    on netbsd
    my problem is I cannot find where in dump.c I can get the flag value
    of system call OPEN I only can find how to get buff (such as
    open("testprg.txt", 0x202, 0x8049bb0) so, I have to cut this for get
    the flag value (0x202) and I would like to check if it is the O_RDWR
    flag which in fcntl.h define to 0x00000002
    so I use & operator.
    I test if (0x00000002 & 0x202) it true.
    So that means it flag O_RDWR but what I get is string '0x202' so I
    cannot..use (0x00000002 & strsysc[2])
    I try if (0x00000002 & (int)strtol(strsysc[2], NULL, 16)) it still
    tell me runtime error Memory fault


    > >
    > >Please give me suggestion about this case
    > >
    > >Regards,
    > >Pattanawadee

    >
    >
    >
    > <<Remove the del for email>>
    pattanawadee, Sep 22, 2003
    #4
  5. On 22 Sep 2003 09:11:52 -0700,
    (pattanawadee) wrote:

    >> Did you mean atoi(strsysc[2])? The statement
    >> if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
    >> should test the bits of the value represented by strsysc[2].

    >
    > I try test this line follow your suggestion but it still tell me
    > on runtime error that 'Memory Falut'


    You need to provide a reasonably small (less than 500 lines)
    compilable example that demonstrates the problem you are having.


    <<Remove the del for email>>
    Barry Schwarz, Sep 23, 2003
    #5
  6. pattanawadee

    pattanawadee Guest

    Dear Sir,

    Now I can solve my problem. It's my fault I don't think that
    sometime the value of strsysc[2] is NULL so, it can make error like
    that.
    Thank you so much for your good suggestion
    if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
    that help me so much for my problem

    Best Regards,
    Pattanawadee

    Barry Schwarz <> wrote in message news:<bkoeql$kp5$1@216.39.135.80>...
    > On 22 Sep 2003 09:11:52 -0700,
    > (pattanawadee) wrote:
    >
    > >> Did you mean atoi(strsysc[2])? The statement
    > >> if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
    > >> should test the bits of the value represented by strsysc[2].

    > >
    > > I try test this line follow your suggestion but it still tell me
    > > on runtime error that 'Memory Falut'

    >
    > You need to provide a reasonably small (less than 500 lines)
    > compilable example that demonstrates the problem you are having.
    >
    >
    > <<Remove the del for email>>
    pattanawadee, Sep 23, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper

    How to create these variables?

    Shapper, Apr 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    305
    Shapper
    Apr 22, 2005
  2. Alex Vinokur
    Replies:
    4
    Views:
    3,035
    Peter Koch Larsen
    Nov 26, 2004
  3. Replies:
    9
    Views:
    931
  4. Ioannis Vranos
    Replies:
    8
    Views:
    327
    James Kanze
    Nov 14, 2008
  5. Lowell Kirsh
    Replies:
    5
    Views:
    110
    Lowell Kirsh
    Aug 18, 2005
Loading...

Share This Page