473,386 Members | 1,699 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,386 software developers and data experts.

strstr and const problem

I am trying to use the strstr function but it doesn't seem to work with a
dynamically allocated string that I pass into a function.

Specifically, I am using a Macromedia C-level extensibility and the
JavaScript interpreter to write an extension.

http://livedocs.macromedia.com/dream...e2.htm#wp80297

http://livedocs.macromedia.com/dream...tml/wwhelp.htm
While the example in the link above doesn't input 2 strings for me to do a
strstr function, my custom function does.

So something like this seem to work:

char const *stringBIG, *strSUB;
char *stringBIGIndex;
stringBIGIndex= strstr("this is a test", "test"); // this is a const and
it works

HOWEVER, this doesn't seem to work:

char const *stringBIG, *strSUB;
char *stringBIGIndex;
unsigned int strSUBlen, stringBIGlen;

// Convert the stringBIG to a string
stringBIG = JS_ValueToString(cx, argv[0], &stringBIGlen);

strSUB= JS_ValueToString(cx, argv[1], &strSUBlen);

stringBIGIndex= strstr(*stringBIG , *strSUB);
So, if you have a spelled out hard coded constant string, like "this is a
test", it works. But if I want to pass a string dynamically in, convert it
and use the strstr function it doesn't.

I also tried declaring the line
char const *stringBIG, *strSUB;

previously without the const keyword
char *stringBIG, *strSUB;

And that didn't seem to work originally, so I added the const in there.
Still no success.

I am trying to use the <string.h> library but I can't seem to use it in
practical manner.
Thanks.




Jun 6 '06 #1
4 5382
smnoff said:

<snip>
char const *stringBIG, *strSUB;
<snip>
stringBIGIndex= strstr(*stringBIG , *strSUB);


strstr takes const char *, not const char. If stringBIG and strSUB are
pointing to valid strings, then you just need to do this:

stringBIGIndex= strstr(stringBIG , strSUB);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 6 '06 #2
smnoff wrote:
I am trying to use the strstr function but it doesn't seem to work with a
dynamically allocated string that I pass into a function.

Specifically, I am using a Macromedia C-level extensibility and the
JavaScript interpreter to write an extension.

http://livedocs.macromedia.com/dream...e2.htm#wp80297

http://livedocs.macromedia.com/dream...tml/wwhelp.htm
While the example in the link above doesn't input 2 strings for me to do a
strstr function, my custom function does.

So something like this seem to work:

char const *stringBIG, *strSUB;
char *stringBIGIndex;
stringBIGIndex= strstr("this is a test", "test"); // this is a const and
it works

HOWEVER, this doesn't seem to work:

char const *stringBIG, *strSUB;
char *stringBIGIndex;
unsigned int strSUBlen, stringBIGlen;

// Convert the stringBIG to a string
stringBIG = JS_ValueToString(cx, argv[0], &stringBIGlen);

strSUB= JS_ValueToString(cx, argv[1], &strSUBlen);

stringBIGIndex= strstr(*stringBIG , *strSUB);
Should be:
stringBIGIndex = strstr(stringBIG, strSUB);

Robert

So, if you have a spelled out hard coded constant string, like "this is a
test", it works. But if I want to pass a string dynamically in, convert it
and use the strstr function it doesn't.

I also tried declaring the line
char const *stringBIG, *strSUB;

previously without the const keyword
char *stringBIG, *strSUB;

And that didn't seem to work originally, so I added the const in there.
Still no success.

I am trying to use the <string.h> library but I can't seem to use it in
practical manner.
Thanks.



Jun 6 '06 #3
It worked when I didn't include the * in strstr as you recommended. Thanks.

Now, I am trying to understand why it worked. I guess when I used the line:

stringBIGIndex= strstr(*stringBIG , *strSUB);

I was already dereferencing to the value? As opposed to just sticking in a
auto allocated variable name?

I guess when I declared this line
char const *stringBIG, *strSUB;

I don't have to separately declare:

char const stringBIG, strSUB; // no * pointer here and because of the
first one above, why?

I know it does it, but don't know how and why it does it.

Can someone please explain?

"Richard Heathfield" <in*****@invalid.invalid> wrote in message
news:OP********************@bt.com...
smnoff said:

<snip>
char const *stringBIG, *strSUB;


<snip>
stringBIGIndex= strstr(*stringBIG , *strSUB);


strstr takes const char *, not const char. If stringBIG and strSUB are
pointing to valid strings, then you just need to do this:

stringBIGIndex= strstr(stringBIG , strSUB);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Jun 6 '06 #4
av
On Tue, 6 Jun 2006 10:54:56 -0500, "smnoff" <rh******@hotmail.com>
wrote:
I am trying to use the strstr function but it doesn't seem to work with a
dynamically allocated string that I pass into a function.

Specifically, I am using a Macromedia C-level extensibility and the
JavaScript interpreter to write an extension.

http://livedocs.macromedia.com/dream...e2.htm#wp80297

http://livedocs.macromedia.com/dream...tml/wwhelp.htm
While the example in the link above doesn't input 2 strings for me to do a
strstr function, my custom function does.

So something like this seem to work:

char const *stringBIG, *strSUB;
char *stringBIGIndex;
stringBIGIndex= strstr("this is a test", "test"); // this is a const and
it works


it seems to me str* name are reserved for the compiler
so the above is wrong
Jun 7 '06 #5

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

Similar topics

3
by: Sean Bartholomew | last post by:
im trying to parse my email file located in my system library on my mac osx. i have a form online that, when filled and submitted, is sent to my email address. i need to parse the fields that...
18
by: Ramprasad A Padmanabhan | last post by:
In my program I am checking wether a emailid exists in a list I have in the complete_list a string like "<aa@abc.com> <ab@abc.com> <bc@abc.com> ... <xy@abc.com>" that is a string of emailids...
12
by: Lars Langer | last post by:
Hi there, I'm new to this C - never the less - I'm trying to search a string for the occurence of a substring - However, I'm not very succesful since my use of the strstr function always returns...
24
by: John Smith | last post by:
How to test whether strstr() returns a null pointer or not? Do I use something like the following? if(NULL != strstr(str1,str2)) That does not seem to work.
5
by: OzBob | last post by:
Am performing the following check to determine if a set of text is at the start of a string,.... /* Check for literal text DD at beginning of string date_format */ char date_format; if...
13
by: Kelvin Moss | last post by:
Hi all, How could one write an strstr function to work with unicode characters? Are there existing implementations/solutions/api for doing so? Any pointers would be appreciated. Thanks ..
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
3
by: lovecreatesbea... | last post by:
I'm getting `warning: return discards qualifiers from pointer target type' at line 11 on this code from gcc. Some people suggested in old posts that this kind of warning can be suppressed by...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.