D
DSF
Hello all,
I want to thank all of those who responded to my previous "Pointer
question" post. The reason I am starting a new thread is that I had a
heck of a time finding my post in the first place. It was attached to
a post titled "Pointer question" from July 28 of 2004! I have to
remember from now on to try to be less generic in the naming of my
posts.
)
From the replies I received, I gather there is no guaranteed way to
compare pointers against each other to check if they are in range.
And even though it causes no problems on my system, and the odds are
pretty small it will ever be used on a system where it would be
problematic, I would like to get it right for the sake of getting it
right. After all, that is why I asked here.
For the record, I have written programs on systems where there was
no such thing as an invalid pointer. Or maybe I should say an illegal
pointer. A pointer not pointing to where it should could be
considered invalid, but not necessarily illegal.
As for the name, I do seem to remember reading that str* is
reserved. It makes sense to follow that convention. I ran afoul of
it all by myself. I spent four or five minutes trying to find the
documentation for strpad, only to discover I wrote it.
Anyway, here is the updated code. Look better?
int StringDelete(char *str, size_t pos, size_t n)
{
char *p1;
char *p2;
size_t l = strlen(str);
if(pos < l)
{
if(pos + n > l)
n = l - pos;
p1 = str + pos;
p2 = p1 + n;
while(*p2)
*p1++ = *p2++;
*p1 = *p2;
return 0;
}
return 1;
}
DSF
I want to thank all of those who responded to my previous "Pointer
question" post. The reason I am starting a new thread is that I had a
heck of a time finding my post in the first place. It was attached to
a post titled "Pointer question" from July 28 of 2004! I have to
remember from now on to try to be less generic in the naming of my
posts.
From the replies I received, I gather there is no guaranteed way to
compare pointers against each other to check if they are in range.
And even though it causes no problems on my system, and the odds are
pretty small it will ever be used on a system where it would be
problematic, I would like to get it right for the sake of getting it
right. After all, that is why I asked here.
For the record, I have written programs on systems where there was
no such thing as an invalid pointer. Or maybe I should say an illegal
pointer. A pointer not pointing to where it should could be
considered invalid, but not necessarily illegal.
As for the name, I do seem to remember reading that str* is
reserved. It makes sense to follow that convention. I ran afoul of
it all by myself. I spent four or five minutes trying to find the
documentation for strpad, only to discover I wrote it.
Anyway, here is the updated code. Look better?
int StringDelete(char *str, size_t pos, size_t n)
{
char *p1;
char *p2;
size_t l = strlen(str);
if(pos < l)
{
if(pos + n > l)
n = l - pos;
p1 = str + pos;
p2 = p1 + n;
while(*p2)
*p1++ = *p2++;
*p1 = *p2;
return 0;
}
return 1;
}
DSF