Learning pointers

M

Markus

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
 
J

jacob navia

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
 
P

pete

Markus said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Adding adressing of IPv6 to program 1
Help with pointers 1
A generic interface for numeric variables 8
wcstombs() problem 16
TF-IDF 1
Segmentation fault when using strtok 4
gcc inline memcpy 7
Working with files 1

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top