Problems whith an header file

J

J4CK4L

Hi everyone,
I've just created a function in a header file that accept a string and
parse but the prototipe of the function return me an exception:

#include <stdio.h>
#include <string.h>

int Scan(char Search[]) // this generate the exception
{
// char Search[] = "Sample text"; //only in this way
that perfectly works!
char *pch;
pch = strtok (Search," -,.");
while (pch != NULL)
{
printf ("%s\n",pch);
//
// some other code here...
//
pch = strtok (NULL, " -,.");
}
}


the function must accept the string.
I use this header whith this sample.cpp


#include "myHeader.h"

int main()
{
Scan("Hi everyone");
return 0;
}


the function return an error when i try to parse a string whith " -,."
Can you help me please?
Thx for all!
Federico
 
J

Jack Klein

Hi everyone,
I've just created a function in a header file that accept a string and
parse but the prototipe of the function return me an exception:

What exception? Always include the compiler, linker, or run time
error message in your post. Don't expect people to read your mind or
guess what the problem is.
#include <stdio.h>
#include <string.h>

Is this C code or C++? In C++, it is much better to use <cstdio> and
int Scan(char Search[]) // this generate the exception
{
// char Search[] = "Sample text"; //only in this way
that perfectly works!
char *pch;
pch = strtok (Search," -,.");

The strtok() function modified the string, that is it writes a '\0'
over each token as it finds it.
while (pch != NULL)
{
printf ("%s\n",pch);
//
// some other code here...
//
pch = strtok (NULL, " -,.");
}
}


the function must accept the string.
I use this header whith this sample.cpp


#include "myHeader.h"

int main()
{
Scan("Hi everyone");

You are passing a string literal to your function. It is undefined
behavior, in both C and C++, to try to modify a string literal. Try
changing your code like this:

char test [] = "Hi everyone";
Scan(test);
return 0;
}


the function return an error when i try to parse a string whith " -,."
Can you help me please?

The array test is an array of writeable characters that strtok() can
modify.

See also this specific question in the comp.lang.c FAQ:

http://c-faq.com/decl/strlitinit.html
 
J

J4CK4L

Thx for all!!
I just made it!
Oh... another question;
how can I see if the word that I scan is match whit another in a file
called wordlist.dat?
thx again! :)
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top