• C

Having trouble using the String.h functions with the '/' character

When I execute the code (posted below), on the following line:
"www.hostname.com/ blahblahblah"

It works fine doing the comparisons, up until it gets to the '/' character, at which point I get a seg fault.  Why is this??
while(strncmp(ln, " ", 1) != 0)
    {
      printf("LN at current position: %s \n", ln);
      ln++;
    }

Open in new window

jonathanjeffreyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
What are you trying to do? If you want to locate the first blank in a string, you will be better off using 'strchr()', e.g.

char test[] = "www.hostname.com/ blahblahblah";
char* ln;

ln = strchr(test,' ');

See also http://cplusplus.com/reference/clibrary/cstring/strchr.html
0
jonathanjeffreyAuthor Commented:
I was using that, and I also got the seg fault.  Which is why I tried using strncmp...
0
jonathanjeffreyAuthor Commented:
Even if I just stick it in main as in code snippet below, I still get the seg fault.  

int main(void)
{
  int i = 0;
  char * ln = "www.hostname.com/ BLAHBLAH";
 
  while(strncmp(ln, " ", 1) !=0)
    ln++;
 
  return 0;
}

Open in new window

0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

jkrCommented:
That's odd. The following compiles and runs fine:


#include <string.h>
 
int main(void)
{
  int i = 0;
  char * ln = "www.hostname.com/ BLAHBLAH";
 
  while(strncmp(ln, " ", 1) !=0)
    ln++;
 
  return 0;
}

Open in new window

0
cupCommented:
Just guessing here - have you got at \ instead of a /
0
ozoCommented:
after it gets to the '/' character, it will get to the ' ' character, and the loop should exit,
which would expose you to any seg fault that the following code may produce.
If there is no ' ' character, the loop will never exit, and you will eventually get a seg fault when ln++ becomes invalid
0
evilrixSenior Software Engineer (Avast)Commented:
Are you 100% sure the problem is with  this bit of code? The fact that strchr also fails suggests to me that this code is fine but previous code is defective and has maybe trampled over your stack, thus modifying the contents of this buffer so it doesn't contain what you think it does. Are you able to post more code than this small snippet for review?

-Rx.
0
jonathanjeffreyAuthor Commented:
when I run that second little main (11 lines) as a program all on its own, i get the same error.
0
evilrixSenior Software Engineer (Avast)Commented:
I cannot reproduce your problem using the code from #20806507. Are you able to try this on a different compiler/platform? What compiler (with version) & platform are you using? It's possible (I have been there myself) it's a compiler bug.
0
evilrixSenior Software Engineer (Avast)Commented:
Below are a number of different ways of doing this that all work on both my Windows and Linux platforms (so hopefully I've not overlooked any silly mistakes in my own code :) ). The output you should see is below. Can you see if they work for you? If you have any failure can you post your output here?

NB. These are not meant to represent ways I'd advocate doing this (although I'd probably do it the strchr() way); rather, this is just to try and help diagnose the problem.

[output]

using_strncmp()
Skipped: w
Skipped: w
Skipped: w
Skipped: .
Skipped: h
Skipped: o
Skipped: s
Skipped: t
Skipped: n
Skipped: a
Skipped: m
Skipped: e
Skipped: .
Skipped: c
Skipped: o
Skipped: m
Skipped: /
Success: BLAHBLAH


using_dereferencing()
Skipped: w
Skipped: w
Skipped: w
Skipped: .
Skipped: h
Skipped: o
Skipped: s
Skipped: t
Skipped: n
Skipped: a
Skipped: m
Skipped: e
Skipped: .
Skipped: c
Skipped: o
Skipped: m
Skipped: /
Success: BLAHBLAH


using_forloop()
Skipped: w
Skipped: w
Skipped: w
Skipped: .
Skipped: h
Skipped: o
Skipped: s
Skipped: t
Skipped: n
Skipped: a
Skipped: m
Skipped: e
Skipped: .
Skipped: c
Skipped: o
Skipped: m
Skipped: /
Success: BLAHBLAH


using_strchr()
Success: BLAHBLAH

[/output]
#include <string.h>
#include <stdio.h>
 
char const TESTDATA[] = "www.hostname.com/ BLAHBLAH";
size_t const TESTDATA_LEN = sizeof(TESTDATA)/sizeof(TESTDATA[0]);
 
void process_result(char const * ln)
{
	if(ln && (' ' == *ln)) {
		printf("Success: %s\n", ++ln);
	} else {
		printf("!!!Something bad happened!!!\n");
	}
 
	printf("\n\n");
}
 
void using_strncmp() {
	char const * ln = TESTDATA;
 
	printf("using_strncmp()\n");
 
	while(strncmp(ln, " ", 1))	{
		printf("Skipped: %c\n", *ln);
		if(TESTDATA_LEN == (++ln - TESTDATA)) { break; }
	}
 
	process_result(ln);
}
 
void using_dereferencing() {
	char const * ln = TESTDATA;
 
	printf("using_dereferencing()\n");
 
	while(*ln != ' ')	{
		printf("Skipped: %c\n", *ln);
		if(TESTDATA_LEN == (++ln - TESTDATA)) { break; }
	}
 
	process_result(ln);
}
 
void using_indexer() {
	char const * ln = TESTDATA;
	size_t pos = 0;
 
	printf("using_forloop()\n");
 
	for(; (ln[pos] != ' ') && (pos < TESTDATA_LEN) ; ++pos) {
		printf("Skipped: %c\n", ln[pos]);
	}
 
	process_result(&ln[pos]);
}
 
void using_strchr() {
	char const * ln = strchr(TESTDATA, ' ');
	printf("using_strchr()\n");
	process_result(ln);
}
 
int main(void)
{
	/* Comment out each that doesn't work (they all should!) */
	using_strncmp();
	using_dereferencing();
	using_indexer();
	using_strchr();
 
	return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jonathanjeffreyAuthor Commented:
Ok, ive been working on it and I think my problem is that I have completely forgotten how Strings/Char arrays work in C.  Its been several years since I've used them.  

Here is what I am trying to accomplish.  I will have a variable length string, in the following format:
char mystring[100];
mystring = "XXXX AAAABBBB CCCC"

I want to make function(s) that do the following:
-I want to extract AAAA from the string and put it into a new string.
-I also want to extract BBBB from the string and put it in a (different) new string.

I think I am fine using the string library to extract the AAAA's and BBBB's, but im not sure how I should set up my functions.   I'm not sure how to create/store the new strings properly though, and I think thats how i keep getting seg. faults.  Any suggestions?
0
Infinity08Commented:
>> char mystring[100];
>> mystring = "XXXX AAAABBBB CCCC"

Make that :

        char mystring[100] = "XXXX AAAABBBB CCCC";


>> -I want to extract AAAA from the string and put it into a new string.

Where does AAAA end, and BBBB begin ? Is there a specific length ? A specific character ?
0
ozoCommented:
mystring in
char mystring[100];
is a constant (the address of the first character in the array)
and cannot be assigned to with
mystring = "XXXX AAAABBBB CCCC"
You might have meant either
char *mystring;
mystring = "XXXX AAAABBBB CCCC";
or
char mystring[100];
strcpy(mystring, "XXXX AAAABBBB CCCC");


0
jonathanjeffreyAuthor Commented:
Yes, the mystring buffer will actually have been filled previously by the recv() function.  
As I said in my last post, I can get at the parts of the string I want, but I'm not sure how do design my functions.  What I mean is.

Should I have 1 or 2 functions which get the 2 different strings out?
What should my function(s) return?  a char *ptr ?
0
Infinity08Commented:
>> Should I have 1 or 2 functions which get the 2 different strings out?
>> What should my function(s) return?  a char *ptr ?

That depends entirely on your requirements. Things like : can the original string be modified ? What will you need the extracted strings for ? Where is the split between AAAA and BBBB ? In what context will this be used ? ...
0
ozoCommented:
when you have determined where AAAA begins and ends, and where you want to put it, you might use strncat
0
evilrixSenior Software Engineer (Avast)Commented:
So, no feedback to http#20808498 ?
0
evilrixSenior Software Engineer (Avast)Commented:
So, no feedback to  http:#20808498 ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.