Learning pointers

Discussion in 'C Programming' started by Markus, Jul 22, 2007.

  1. Markus

    Markus Guest

    I am hoping I'm in the right place.

    Please help me with my pointer understanding... Using a slightly
    altered strcmp function I come up with this:

    int mystrcmp ( const char * src, const char * dst)
    {
    int ret = 0 ;
    while( 0 == ret && *dst)
    {
    ret = (*(unsigned char *)src - *(unsigned char *)dst);
    ++src;
    ++dst;
    }
    .....
    }

    In regards to just the pointers, it is my understanding that:
    1. I will come into this function with addresses to both parameters
    (src and dst) which are of type const char.
    2. I then go into the while loop and 'cast' the type to unsigned char
    for ease of ASCII arithmetic (something like that).
    3. Then each the addresses are incremented by one with the ++src
    etc., revealing the contents of each to be compared again.

    What I'm not understanding, is the * outside of the parans...IE:
    ^*^(unsigned char *)src - ^*^(unsigned char *)dst
    Doesn't the (unsigned char *) point to and dereference the initial and
    subsequent addresses???

    Thanks for any help,
    Markus
    Markus, Jul 22, 2007
    #1
    1. Advertising

  2. Markus

    jacob navia Guest

    Markus wrote:
    [snip]
    > What I'm not understanding, is the * outside of the parans...IE:
    > ^*^(unsigned char *)src - ^*^(unsigned char *)dst
    > Doesn't the (unsigned char *) point to and dereference the initial and
    > subsequent addresses???
    >
    > Thanks for any help,
    > Markus


    *(unsigned char *)src

    accesses the value contained in the address pointed by src
    as an unsigned char, i.e. it will not sign extend the character
    when transforming it to an integer to make the subtraction.

    Since the argument "src" was a char *, NOT an unsigned char pointer,
    the cast
    (unsigned char *)src

    makes sure that the value in src remains positive but it does NOT
    dereference it. It is the "*" precisely, that makes that. Review
    your C operators.

    jacob
    jacob navia, Jul 22, 2007
    #2
    1. Advertising

  3. Markus

    pete Guest

    Markus wrote:
    >
    > I am hoping I'm in the right place.
    >
    > Please help me with my pointer understanding... Using a slightly
    > altered strcmp function I come up with this:
    >
    > int mystrcmp ( const char * src, const char * dst)
    > {
    > int ret = 0 ;
    > while( 0 == ret && *dst)
    > {
    > ret = (*(unsigned char *)src - *(unsigned char *)dst);
    > ++src;
    > ++dst;
    > }
    > ....
    > }


    One obvious problem is that
    mystrcmp( X, "")
    returns zero, for any and all pointer values of X.

    > In regards to just the pointers, it is my understanding that:
    > 1. I will come into this function with addresses to both parameters
    > (src and dst) which are of type const char.


    Also they have to be pointers to strings.

    (const unsigned char *) would have been a better choice
    than (unsigned char *).
    It's bad style to cast away constness without a reason.

    > 2. I then go into the while loop and 'cast' the type to unsigned char
    > for ease of ASCII arithmetic (something like that).


    The specification for strcmp, is for the bytes to be compared
    as unsigned char. ASCII is not part of the reason.
    strcmp(a,b) and memcmp(a,b,c) give the same result
    when c is equal to (1 + length of the shorter string).

    My prference for comparison is the conditional operator:

    const unsigned char *p1 = (const unsigned char *)s1;
    const unsigned char *p2 = (const unsigned char *)s2;

    if (*p1 != *p2) {
    return *p2 > *p1 ? -1 : 1;

    If int can't represent all of the values of unsigned char
    (that possibility is allowed),
    then the result of (*(unsigned char *)src - *(unsigned char *)dst)
    will be positive,
    no matter what values are in the bytes being compared;
    If that resulting value in not in the range of int,
    then assigning that value to ret will be implementation defined.

    > 3. Then each the addresses are incremented by one with the ++src
    > etc., revealing the contents of each to be compared again.


    "compared again" is right,
    but you have not mentioned comparing them prior to this point.

    > What I'm not understanding, is the * outside of the parans...IE:
    > ^*^(unsigned char *)src - ^*^(unsigned char *)dst
    > Doesn't the (unsigned char *) point to and dereference the initial and
    > subsequent addresses???


    No. The cast just converts the type of the pointer
    expression to another pointer type.

    --
    pete
    pete, Jul 22, 2007
    #3
  4. Markus

    Markus Guest

    On Sun, 22 Jul 2007 02:11:05 -0700, Markus <>
    wrote:

    Jacob and Pete: Thanks!

    Markus
    Markus, Jul 22, 2007
    #4
    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. Javier

    Learning pointers in C++

    Javier, Feb 3, 2005, in forum: C++
    Replies:
    17
    Views:
    2,067
    Noah Roberts
    Feb 3, 2005
  2. Replies:
    1
    Views:
    633
    -berlin.de
    Mar 28, 2005
  3. Hal Vaughan
    Replies:
    7
    Views:
    464
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    691
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page