473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a better way to do this?

Hi.

I did this in a C program, which involves searching for the occurence
of a string
in another string:

---
posptr = strstr(MyString, StringToFind);

if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */
---

("MyString" and "StringToFind" are both arrays of type char and null-
terminated.)

It's that last part I'm wondering about. Is that part "legal" in
standard C? Safe?
If not, what's a more "legal" or "safe" method to accomplish what I'm
trying
to accomplish with this?

Sep 21 '07 #1
6 1402
mike3 wrote:
Hi.

I did this in a C program, which involves searching for the occurence
of a string
in another string:

---
posptr = strstr(MyString, StringToFind);

if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */
---

("MyString" and "StringToFind" are both arrays of type char and null-
terminated.)

It's that last part I'm wondering about. Is that part "legal" in
standard C? Safe?
If not, what's a more "legal" or "safe" method to accomplish what I'm
trying
to accomplish with this?
1. /sizeof(char) is redundant since sizeof(char)==1. However, you may
want to keep it your way if it adds to clarity.
2. StringToFind - &MyString[0] is illegal (pointers NOT to the same
array); you may have meant
posptr - &MyString[0]
which is the same as
posptr - MyString
and is of type ptrdiff_t which may be wider than int. How about this:
ptrdiff_t ret = posptr - MyString;
assert(ret == (int)ret);
return (int)ret;
-- Ark
Sep 21 '07 #2
mike3 <mi******@yahoo.comwrites:
I did this in a C program, which involves searching for the
occurence of a string in another string:

---
posptr = strstr(MyString, StringToFind);

if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */
Several comments on that last statement:

* Most importantly, StringToFind and &MyString[0] do not
point within the same array, so the result of the
subtraction is undefined. Fortunately, it seems that
you really just made a small mistake here: I think that
you really want to subtract &MyString[0] from posptr,
not from StringToFind.

* &MyString[0] and MyString have the same type and value,
except in a few specific contexts, and so you might as
well write the latter.

* The cast to int is gratuitous. The result of
subtracting one pointer from another is already a
signed integer type (often int). Casting this result
to int will either have no effect on its value (the
common case) or yield undefined behavior (if the result
is out of the range of int), so there's no point in
doing it.

* Division by sizeof(char) is also gratuitous, because
sizeof(char) is always 1 by definition.

Thus, you can correctly write this statement as simply:
return posptr - MyString;
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Sep 21 '07 #3
On Sep 20, 9:10 pm, Ark Khasin <akha...@macroexpressions.comwrote:
mike3 wrote:
Hi.
I did this in a C program, which involves searching for the occurence
of a string
in another string:
---
posptr = strstr(MyString, StringToFind);
if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */
---
("MyString" and "StringToFind" are both arrays of type char and null-
terminated.)
It's that last part I'm wondering about. Is that part "legal" in
standard C? Safe?
If not, what's a more "legal" or "safe" method to accomplish what I'm
trying
to accomplish with this?

1. /sizeof(char) is redundant since sizeof(char)==1. However, you may
want to keep it your way if it adds to clarity.
2. StringToFind - &MyString[0] is illegal (pointers NOT to the same
array); you may have meant
posptr - &MyString[0]
Sorry, yes that is what I meant.
which is the same as
posptr - MyString
and is of type ptrdiff_t which may be wider than int. How about this:
ptrdiff_t ret = posptr - MyString;
assert(ret == (int)ret);
return (int)ret;
Alright.
-- Ark

Sep 21 '07 #4
On Sep 20, 9:14 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
mike3 <mike4...@yahoo.comwrites:
I did this in a C program, which involves searching for the
occurence of a string in another string:
---
posptr = strstr(MyString, StringToFind);
if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */

Several comments on that last statement:

* Most importantly, StringToFind and &MyString[0] do not
point within the same array, so the result of the
subtraction is undefined. Fortunately, it seems that
you really just made a small mistake here: I think that
you really want to subtract &MyString[0] from posptr,
not from StringToFind.

* &MyString[0] and MyString have the same type and value,
except in a few specific contexts, and so you might as
well write the latter.

* The cast to int is gratuitous. The result of
subtracting one pointer from another is already a
signed integer type (often int). Casting this result
to int will either have no effect on its value (the
common case) or yield undefined behavior (if the result
is out of the range of int), so there's no point in
doing it.

* Division by sizeof(char) is also gratuitous, because
sizeof(char) is always 1 by definition.

Thus, you can correctly write this statement as simply:
return posptr - MyString;
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Thanks for the response. I'll do that.

Sep 21 '07 #5
mike3 wrote:
On Sep 20, 9:10 pm, Ark Khasin <akha...@macroexpressions.comwrote:
>mike3 wrote:
>>Hi.
I did this in a C program, which involves searching for the occurence
of a string
in another string:
---
posptr = strstr(MyString, StringToFind);
if(posptr == NULL) return(-1); /* Didn't find it */
return(((int)(StringToFind - &MyString[0]))/sizeof(char)); /* turn
returned pointer into a zero-indexed character offset */
---
("MyString" and "StringToFind" are both arrays of type char and null-
terminated.)
It's that last part I'm wondering about. Is that part "legal" in
standard C? Safe?
If not, what's a more "legal" or "safe" method to accomplish what I'm
trying
to accomplish with this?
1. /sizeof(char) is redundant since sizeof(char)==1. However, you may
want to keep it your way if it adds to clarity.
2. StringToFind - &MyString[0] is illegal (pointers NOT to the same
array); you may have meant
posptr - &MyString[0]

Sorry, yes that is what I meant.
>which is the same as
posptr - MyString
and is of type ptrdiff_t which may be wider than int. How about this:
ptrdiff_t ret = posptr - MyString;
assert(ret == (int)ret);
return (int)ret;

Alright.
>-- Ark

On a second thought, /sizeof(char) is plainly misleading, rather than
"redundant" or "gratuitous": a difference of two pointers is already
scaled to the size of the objects pointed to.
e.g,
T foo[];
&foo[5]-&foo[0] == 5
whatever the type T is.
-- Ark
Sep 21 '07 #6
On Fri, 21 Sep 2007 03:10:25 +0000, Ark Khasin wrote:
mike3 wrote:
ptrdiff_t ret = posptr - MyString;
assert(ret == (int)ret);
I think assert(ret <= INT_MAX) is clearer. (Also I don't think it is
the case to use assert() here...)
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ForHimself Every Man | last post by:
What's better about Rattlesnakes than Python. I'm sure there's something. What is it? This is not a troll. I'm a snake shooping and I want people's answers. I don't know beans about...
10
by: michael newport | last post by:
Dear friends of database(s), After 13 years of Ingres, I am now using Oracle. But is Oracle technically better than Ingres. I would be much obliged if anyone could shed some light on the...
24
by: Faith Dorell | last post by:
I really dont like C.You can write better programs in BASIC than in C, if you dont like this language. I dont understand how C became so popular, although much better programming languages...
39
by: bazad | last post by:
Hi, I am not using C all the time. I have a general understanding of C and nothing else. The recent reply to use strlcpy and strlcat showed me that I am not aware of the best and safe...
2
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
21
by: gavino | last post by:
why?
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
43
by: Pawel_Iks | last post by:
I've read somewhere that c++ is something more than better c ... then I talk with my friend and he claimed that c++ is nothing more than better c ... I tried to explain him that he was wrong but I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.