I need help with the following program I wrote. Im getting a segmentation fault and the warning "assignment makes integer from pointer without cast". Can anyone spot any errors with the program?

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 256

//function portotype
char * next_word(char *instring, char **new_start);


int main ()
{
	char instring[MAXLENGTH];
	int words;
	//char *parsed=;

	printf("Enter text of line");

	while (1)
	{
		fgets(instring, MAXLENGTH, stdin);
		if (instring[0] == '\n')
			break;
		
		//puts(instring);

		//need help with what to do here
		words=next_word(instring, '\0' );
		
		
		return 0;
	}
}


char *next_word(char *instring, char **new_start)
{
	char *token;

	if(instring == NULL)
		instring=*new_start;

	//printf("Adress of instring: %x\n", instring);
	//printf("Addres of newstart: %x\n", *new_start);
	
	instring += strspn(instring, '\0');
	
	//printf("Address of instring after strspn: %x\n", instring);

	if (*instring == '\0');
	return NULL;

	token = instring;
	instring = strpbrk(token, '\0');

	//printf("Address of instring after strpbrk: %x\n", instring);

	if (instring = NULL)
	{
		*new_start = strchr(token, '\0');
	}
	else
	{
		*instring = '\0';
		*new_start= instring +1;
	}
	return token;
}
jephthah commented: used code tags properly on your first post! ( and your screen name is funny :) +11

Recommended Answers

All 10 Replies

>>words=next_word(instring, '\0' );
The function returns char* but words is an integer. The warning should not have been ignored. Fix it instead of ignoring it.

The second thing wrong with that line is that the second argument to the function is supposed to be a char**, not a single character. Learn to be more detailed-oriented and pay attention to what you are doing.

>>words=next_word(instring, '\0' );
The function returns char* but words is an integer. The warning should not have been ignored. Fix it instead of ignoring it.

The second thing wrong with that line is that the second argument to the function is supposed to be a char**, not a single character. Learn to be more detailed-oriented and pay attention to what you are doing.

didnt mean to come off as careless. I fixed the first issue, but im not sure i quite understood the second problem you were pointing out. The program now compiles fine, but I still get a segfault after i enter my string of characters. this is probably what you were referring to

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 256

//function portotype
char * next_word(char *instring, char **new_start);


int main ()
{
	char instring[MAXLENGTH];
	char *words;
	char **parsed='\0';

	printf("Enter text of line");

	while (1)
	{
		fgets(instring, MAXLENGTH, stdin);
		if (instring[0] == '\n')
			break;
		
		//puts(instring);

		//need help with what to do here
		words=next_word(instring, parsed );
		
		
		return 0;
	}
}


char *next_word(char *instring, char **new_start)
{
	char *token;

	if(instring == NULL)
		instring=*new_start;

	//printf("Adress of instring: %x\n", instring);
	//printf("Addres of newstart: %x\n", *new_start);
	
	instring += strspn(instring, '\0');
	
	//printf("Address of instring after strspn: %x\n", instring);

	if (*instring == '\0');
	return NULL;

	token = instring;
	instring = strpbrk(token, '\0');

	//printf("Address of instring after strpbrk: %x\n", instring);

	if (instring = NULL)
	{
		*new_start = strchr(token, '\0');
	}
	else
	{
		*instring = '\0';
		*new_start= instring +1;
	}
	return token;
}

you should have received a warning on line 48 -- remove that semicolon!

You are getting seg fault because line 63 is dereferencing a NULL pointer. The second parameter on line 26 is wrong, as well as the declaration on line 13:

char* parsed = NULL; // only one star, not two
...
// now pass parsed by reference
words=next_word(instring, &parsed );

line 44: What is your intent with that line?? You can't pass NULL as the second parameter to strspn() because it will cause seg fault.

Actually I'm confused about what you want next_word() to do. How is it going to be different from just strtok() ? Or are you required to write your own version of strtok() ? If that is true, then you need to read the description of strtok() so that you can try to duplicate it. The function you have posted does not work.

Actually I'm confused about what you want next_word() to do. How is it going to be different from just strtok() ? Or are you required to write your own version of strtok() ? If that is true, then you need to read the description of strtok() so that you can try to duplicate it. The function you have posted does not work.

Ancient Dragon, I really appreciate your help so far. Basically the only requirement for this program is to use this function:

char * next_word(char *instring, char **new_start);

I tried to mimick the strtok code but I guess I didnt get it right.

Here is a description of what the function is supposed to do:
The function takes as parameters an input string instring and a pointer to a string (that is, pointer to pointer to char).

The function will find the first word in instring, and place a terminating character '\0' after the word in instring. If there are still more characters in the string after the newly placed terminiating character, the returned pointer new_start will be set to point to the first character remaining in the original string that hasn't been examined. If the end of the string was reached, the returned pointer new_string will be set to NULL to indicate there are no more characters in the string to be examined.

The function returns a pointer to the first character of the first word contained in instring. If there are no words contained in instring, the function returns the NULL pointer. Also, the call-by-reference parameter new_start is set to point to the character after the word. This pointer can be used in main() to make another call to next_word() to get the next word in the string until all the words are found.

This is how main() should be written. I'll let you figure out how to write that next_word() function

int main ()
{
    char instring[MAXLENGTH];
    char *words = NULL;
    char* parsed = NULL;
    printf("Enter text of line\n");
    fgets(instring, MAXLENGTH, stdin);
    words = next_word(instring, &parsed);

    while (words != NULL)
    {
        printf("%s\n", words);
        words=next_word(parsed, &parsed);
    }
}

This is how main() should be written. I'll let you figure out how to write that next_word() function

int main ()
{
    char instring[MAXLENGTH];
    char *words = NULL;
    char* parsed = NULL;
    printf("Enter text of line\n");
    fgets(instring, MAXLENGTH, stdin);
    words = next_word(instring, &parsed);

    while (words != NULL)
    {
        printf("%s\n", words);
        words=next_word(parsed, &parsed);
    }
}

Thank you so much for the help, this will definitely give me a good start. Quick question though, for the next_word in the loop, should it be (instring, &parsed) or is (parsed, &parsed) the correct way to have this done?

The first time next_word() is called the first parameter should be instring. After that, the first parameter should be parsed because that is a pointer to the next part of the string in instring. If you type "Now is the time", the first time next_word() is called instring will be "Now is the time", and the function will set parsed pointer to "is the time". The second call the first parameter needs to be "is the time", which is in the parsed pointer, not instring.

The first time next_word() is called the first parameter should be instring. After that, the first parameter should be parsed because that is a pointer to the next part of the string in instring.

Gotcha, thanks again :)

Hi, I worked off your template and I got it to work, but I dont think I did it correctly if that makes any sense. It works as such:
Hello world
Hello
world

Did I do it right? the function doesnt seem to be returning anything

#include <stdio.h>
#include <string.h>
#define MAXLENGTH 256

char *next_word(char *instring, char **new_start);

int main()
{
	char instring[MAXLENGTH];
	char *words=NULL;
	char *parsed=NULL;

	printf("Enter the fucking text:\n");

	fgets(instring, MAXLENGTH, stdin);

	puts(instring);
	words=next_word(instring, &parsed);

	while (words != NULL)
	{
		printf("%s\n", words);
		words=next_word(parsed, &parsed);
	}
}

char *next_word(char *instring, char **new_start)
{
	new_start=strtok(instring, " ");
	//return instring;

}

Did I do it right? the function doesnt seem to be returning anything

The function doesn't return anything because the return statement is commented away. The function returns a junk pointer. I don't think strtok is the best way to do this because you don't have a good way of knowing when there's no more string left without counting the delimiters somewhere in instring.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.