Sponsored Content
Top Forums Programming Combining Strings - Segmentation Fault Post 302106637 by Octal on Sunday 11th of February 2007 01:24:42 AM
Old 02-11-2007
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.
 

9 More Discussions You Might Find Interesting

1. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

2. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

3. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

4. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

5. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

6. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

7. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

8. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

9. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
fmd(1M)                                                   System Administration Commands                                                   fmd(1M)

NAME
fmd - fault manager daemon SYNOPSIS
/usr/lib/fm/fmd/fmd [-V] [-f file] [-o opt=val] [-R dir] DESCRIPTION
fmd is a daemon that runs in the background on each Solaris system. fmd receives telemetry information relating to problems detected by the system software, diagnoses these problems, and initiates proactive self-healing activities such as disabling faulty components. When appropriate, the fault manager also sends a message to the syslogd(1M) service to notify an administrator that a problem has been detected. The message directs administrators to a knowledge article on Sun's web site, http://www.sun.com/msg/, which explains more about the problem impact and appropriate responses. Each problem diagnosed by the fault manager is assigned a Universal Unique Identifier (UUID). The UUID uniquely identifes this particular problem across any set of systems. The fmdump(1M) utility can be used to view the list of problems diagnosed by the fault manager, along with their UUIDs and knowledge article message identifiers. The fmadm(1M) utility can be used to view the resources on the system believed to be faulty. The fmstat(1M) utility can be used to report statistics kept by the fault manager. The fault manager is started automatically when Solaris boots, so it is not necessary to use the fmd command directly. Sun's web site explains more about what capabilities are cur- rently available for the fault manager on Solaris. OPTIONS
The following options are supported -f file Read the specified configuration file prior to searching for any of the default fault manager configuration files. -o opt=value Set the specified fault manager option to the specified value. Fault manager options are currently a Private inter- face; see attributes(5) for information about Private interfaces. -R dir Use the specified root directory for all pathnames evaluated by the fault manager, instead of the default root (/). -V Print the fault manager's version to stdout and exit. EXIT STATUS
The following exit values are returned: 0 Successful completion 1 An error occurred which prevented the fault manager from initializing, such as failure to open the telemetry trans- port. 2 Invalid command-line options were specified. FILES
/etc/fm/fmd Fault manager configuration directory /usr/lib/fm/fmd Fault manager library directory /var/fm/fmd Fault manager log directory ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWfmd | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
svcs(1), fmadm(1M), fmdump(1M), fmstat(1M), syslogd(1M), attributes(5), smf(5) http://www.sun.com/msg/ NOTES
The Fault Manager is managed by the service management facility, smf(5), under the service identifier: svc:/system/fmd:default The service's status can be queried using the svcs(1) command. Administrators should not disable the Fault Manager service. SunOS 5.10 17 Nov 2004 fmd(1M)
All times are GMT -4. The time now is 04:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy