The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Why not a segmentation fault?? lagigliaivan High Level Programming 22 05-21-2008 08:07 AM
Segmentation fault big123456 Linux 0 07-20-2007 02:01 AM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 07:43 AM
Segmentation fault jshaulis AIX 1 06-01-2004 01:16 PM
segmentation fault omran High Level Programming 2 08-01-2003 05:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-10-2007
Registered User
 

Join Date: Feb 2007
Posts: 67
Combining Strings - Segmentation Fault

I am writing a function to combine strings, but when I execute I get a segmentation fault, which doesn't make sence. Segmentation fault is when you try to acess a point of memory that doesn't exist, like trying to acess array[10] when you declared array[7].

Here is the source code:
Code:
#include <stdio.h>
/* #include <string.h> == cheating! */

#define MAXINPUT 1000

char combine(char string1[MAXINPUT], char string2[MAXINPUT]) {
	int count1, count2 = 0;

	while (string1[count1] != '\0')
		++count1;

	while (string2[count2] != '\0')
		string1[count1++] = string2[count2++];
	string1[count1] = '\0';
}
main() {
	char string1[MAXINPUT];
	char string2[MAXINPUT];
	int i = 0;
	
	printf("Enter two strings each under 1000 characters:\n");
	while(i = 0) {
		printf("1: ");
		scanf("%s", string1);
		if ((sizeof string1) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
			i = 1;
	}
	i = 0;
	while(i = 0) {
		printf("\n2: ");
		scanf("%s", string2);
		if ((sizeof string2) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
			i = 1;
	}
	printf("%s and %s combined is %s", string1, string2, combine(string1, string2));
	return 0;
}
I know there are functions in the string.h header, but as you can see, I wanted to do it without them, thus calling those functions "cheating".

Thanks for reading.
Reply With Quote
Forum Sponsor
  #2  
Old 02-10-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
(sizeof string1) is a constant and will not vary depending on the contents of string1. And what if 1200 characters were entered? Where do you think the extra 200 will be? You seem to be intentionally blowing arrays.

But your problem is the combine routine. It returns a character, not a string. Or it would had you returned *anything*. Then you use this character as a pointer to character. It looks like you might mean for combine to return a pointer to character. If so, define it that way and put in a return statement.
Reply With Quote
  #3  
Old 02-10-2007
Registered User
 

Join Date: Feb 2007
Posts: 67
Quote:
Originally Posted by Perderabo
But your problem is the combine routine. It returns a character, not a string. Or it would had you returned *anything*. Then you use this character as a pointer to character. It looks like you might mean for combine to return a pointer to character. If so, define it that way and put in a return statement.
I'm sorry, but what does that have to do with the segmentation fault?
Reply With Quote
  #4  
Old 02-10-2007
milhan's Avatar
Registered User
 

Join Date: Oct 2002
Location: /home
Posts: 121
1.st problem:
Quote:
Originally Posted by Octal
Here is the source code:
Code:
char combine(char string1[MAXINPUT], char string2[MAXINPUT]) {
	int count1, count2 = 0;

	while (string1[count1] != '\0')
		++count1;

	while (string2[count2] != '\0')
		string1[count1++] = string2[count2++];
	string1[count1] = '\0';
}
the function is declared as returning char; however there is no return statement that actually returns a char. In this case the function will return a "garbage" value even if the syntax is not necessarily incorrect!

2.nd problem:
Code:
..
	
	printf("Enter two strings each under 1000 characters:\n");
	while(i = 0) {   // should be while (i == 0)
		printf("1: ");
		scanf("%s", string1);
		if ((sizeof string1) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
			i = 1;
	}
	i = 0;
	while(i = 0) {   // should be while (i == 0)
		printf("\n2: ");
		scanf("%s", string2);
		if ((sizeof string2) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
...
Reply With Quote
  #5  
Old 02-10-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Quote:
Originally Posted by Octal
I'm sorry, but what does that have to do with the segmentation fault?
Umm, it's the cause.
Reply With Quote
  #6  
Old 02-10-2007
Registered User
 

Join Date: Feb 2007
Posts: 67
Thanks milhan, you explained it better. I still get a segmentation fault that I don't understand, the current source (almost same as before) :
Code:
#include <stdio.h>
/* #include <string.h> == cheating! */

#define MAXINPUT 1000

char combine(char string1[MAXINPUT], char string2[MAXINPUT]) {
	int count1, count2 = 0;

	while (string1[count1] != '\0')
		++count1;

	while (string2[count2] != '\0')
		string1[count1++] = string2[count2++];
	string1[count1] = '\0';
	return string1;
}
main() {
	char string1[MAXINPUT];
	char string2[MAXINPUT];
	int i = 0;
	
	printf("Enter two strings each under 1000 characters:\n");
	while(i == 0) {
		printf("1: ");
		scanf("%s", string1);
		if ((sizeof string1) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
			i = 1;
	}
	i = 0;
	while(i == 0) {
		printf("2: ");
		scanf("%s", string2);
		if ((sizeof string2) > MAXINPUT) {
			i = 0;
			printf("I said under 1000 characters, please enter it again:\n");
		}
		else
			i = 1;
	}
	printf("%s and %s combined is %s", string1, string2, combine(string1, string2));
	return 0;
}
I get an erorr related to
Code:
return string1;
Which is exactly as put:
Code:
combine-strings.c:15: warning: return makes integer from pointer without a cast
so, I assume that I could redeclare string1 as an int, to make the program compile, even though it wouldn't run correctly, because int is numbers(it can be ascii, but that adds up fast), and char is text. So I declare string1 like:
Code:
return (int) string1;
and I get a segmentaton fault after I enter the two strings. I assume that the int was the problem, so I try a few other examples:
Code:
return string1[0];
Which produces segmentation erorr when executed.
Code:
return string1[MAXLINE];
Also produces segmentation erorr when executed.

The odd part is I don't seem to be asking for a part of memory that doesn't exist.
Reply With Quote
  #7  
Old 02-11-2007
milhan's Avatar
Registered User
 

Join Date: Oct 2002
Location: /home
Posts: 121
octal look at your combine(char [], char[]) function again. I didn't see it at my previous post either.
Code:
char combine(char string1[MAXINPUT], char string2[MAXINPUT]) {
	int count1, count2 = 0;   // you didn't initialize count1, so=> int count1 = 0, count2 = 0;
                         
	while (string1[count1] != '\0')
		++count1;

	while (string2[count2] != '\0')
		string1[count1++] = string2[count2++];
	string1[count1] = '\0';
	return string1;
}
Quote:
Code:
return string1[0];
Which produces segmentation erorr when executed.
return string1[0] is correct. The segmentation fault can't come from here. Try running again with the correction in the combine func. and return string1[0]..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0