473,772 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ValueToStrin g(cx, argv[0], &stringBIGle n);

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

stringBIGIndex= strstr(*stringB IG , *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 5416
smnoff said:

<snip>
char const *stringBIG, *strSUB;
<snip>
stringBIGIndex= strstr(*stringB IG , *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(stringBI G , 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_ValueToStrin g(cx, argv[0], &stringBIGle n);

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

stringBIGIndex= strstr(*stringB IG , *strSUB);
Should be:
stringBIGIndex = strstr(stringBI G, 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(*stringB IG , *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*****@invali d.invalid> wrote in message
news:OP******** ************@bt .com...
smnoff said:

<snip>
char const *stringBIG, *strSUB;


<snip>
stringBIGIndex= strstr(*stringB IG , *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(stringBI G , 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******@hotma il.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
2460
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 were filled out ignoring all the ip and complicated stuff around it. for eg. im trying to at least get the first block of text that ONLY includes the fields and their values. char *tempBuf;
18
11852
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 sorted Now I want to find out if another ID "<lm@abc.com>" exists in this list How is it possible to optimize the search given that the complete list
12
3313
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 NULL. But I know that there exsists such a substring, as I can see it with my own two eye when I print out the string to be searched. I added the code of the function (resolveResponse)and how I call this code - Calling the function:
24
12590
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
2507
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 (strstr(date_format, "DD") != date_format) { /* perform swap here */ }
13
14702
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
9452
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
2413
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 qualifying the return type with const. In this code it also can be removed by removing const from the first pointer parameter str. Are there better ways to do it, or does it deserve to do this? /* Search str for sub and return the pointer to the...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.