C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 03:56 PM   #1
Registered User
 
Join Date: Jun 2009
Posts: 20
segfaulting!

Im getting a segmentation fault when I try to run the program and add new message. As soon as I input the 3 values it segfaults. Any help?


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

struct MSG {
	int priority;
	int destination;
	char *message;
	int length;
	struct MSG *nextPtr;
};

typedef struct MSG Message;

int main ()
{
	struct MSG {
		int priority;
		int destination;
		char *message;
		int length;
		struct MSG *nextPtr;
	};

	struct MSG std[100];
	int I;
	int n;
	
	
	
	int choice = 0;


do
{
	printf("1.) Quit\n");
	printf("2.) Transmit next message\n");
	printf("3.) List messages by priority\n");
	printf("4.) Summarize message list\n");
	printf("5.) Add new message\n");
	printf(":");
	scanf("%d", &choice);
	switch(choice)
	{
		case 1:
			return 0;
		case 2:
			//transmit next message
			break;
		case 3:
			//list messages by priority
			break;
		case 4:
			//summarize message list
			break;
		case 5:
			//add new message
		        printf("How many messages would you like to enter?\n");
			scanf("%d", &n);
			printf("Enter a priority, destination, and message:");
			for (I=0; I < n; I++)
			{
				scanf("%d%d%s", &std[i].priority, &std[i].destination, &std[i].message);
			}
			printf("\ntest info:");
			for (I=0; I < n; I++)
			{
				printf("%d%d%s\n", std[i].priority, std[i].destination, std[i].message);
			}
			break;
		default:
			printf("Invalid entry, please try again.\n");
			break;
	}
}while (choice <= 5);

	return 0;
}
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 04:11 PM   #2
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
your message pointer needs to be initialized before stuff can be stored in it
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝

Last edited by ಠ_ಠ; 07-08-2009 at 04:14 PM.
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 04:15 PM   #3
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
Your program is essentially doing this:
Code:
char *s;
scanf("%s", &s);
There are two problems there. First: you're trying to write to a location that has no memory allocated. s points nowhere (or rather, its value is indeterminate, but that's not important). You have to allocate space. You can either make s an array (which allocates the chars for you), or use malloc() to set aside some memory.

Second, you're passing &s, which has type "pointer to pointer to char", but %s expects "pointer to char". Though you've been conditioned to use & with scanf(), it's not for use with strings. Instead:
Code:
/* Either of these allocates space */
char *s = malloc(1024); /* should make sure s is not NULL now */
char a[1024];
/* Both of these are now correct */
scanf("%s", s);
scanf("%s", a);
scanf() isn't safe being used like this, but I presume you'd rather learn about that at a later date. scanf() is actually a pain to use properly for most applications.
cas is offline   Reply With Quote
Old 07-08-2009, 04:24 PM   #4
Registered User
 
Join Date: Jun 2009
Posts: 20
Thanks for the help guys, I have this code now which freezes after I input the values:

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


int main ()
{
	struct MSG {
		int priority;
		int destination;
		char *message;
		int length;
		struct MSG *nextPtr;
	};

	struct MSG std[100];
        char message[100];
	int I;
	int n;
	
	
	
	int choice = 0;


do
{
	printf("1.) Quit\n");
	printf("2.) Transmit next message\n");
	printf("3.) List messages by priority\n");
	printf("4.) Summarize message list\n");
	printf("5.) Add new message\n");
	printf(":");
	scanf("%d", &choice);
	switch(choice)
	{
		case 1:
			return 0;
		case 2:
			//transmit next message
			break;
		case 3:
			//list messages by priority
			break;
		case 4:
			//summarize message list
			break;
		case 5:
			//add new message
		        printf("How many messages would you like to enter?\n");
			scanf("%d", &n);
			printf("Enter a priority, destination, and message:");
			for (I=0; I < n; I++)
			{
				scanf("%d%d%s", &std[i].priority, &std[i].destination, &std[i].message);
			}
			printf("\ntest info:");
			for (I=0; I < n; I++)
			{
				printf("%d%d%s\n", std[i].priority, std[i].destination, std[i].message);
			}
			break;
		default:
			printf("Invalid entry, please try again.\n");
			break;
	}
}while (choice <= 5);

	return 0;
}
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 04:27 PM   #5
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by cas View Post
scanf() isn't safe being used like this, but I presume you'd rather learn about that at a later date. scanf() is actually a pain to use properly for most applications.
Im here to learn so if you have better alternatives id love to hear them
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 04:41 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by CMakesMeSad :( View Post
Thanks for the help guys, I have this code now which freezes after I input the values:

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


int main ()
{
	struct MSG {
		int priority;
		int destination;
		char *message;
		int length;
		struct MSG *nextPtr;
	};

	struct MSG std[100];
        char message[100];
	int I;
	int n;
	
	
	
	int choice = 0;


do
{
	printf("1.) Quit\n");
	printf("2.) Transmit next message\n");
	printf("3.) List messages by priority\n");
	printf("4.) Summarize message list\n");
	printf("5.) Add new message\n");
	printf(":");
	scanf("%d", &choice);
	switch(choice)
	{
		case 1:
			return 0;
		case 2:
			//transmit next message
			break;
		case 3:
			//list messages by priority
			break;
		case 4:
			//summarize message list
			break;
		case 5:
			//add new message
		        printf("How many messages would you like to enter?\n");
			scanf("%d", &n);
			printf("Enter a priority, destination, and message:");
			for (I=0; I < n; I++)
			{
				scanf("%d%d%s", &std[i].priority, &std[i].destination, &std[i].message);
			}
			printf("\ntest info:");
			for (I=0; I < n; I++)
			{
				printf("%d%d%s\n", std[i].priority, std[i].destination, std[i].message);
			}
			break;
		default:
			printf("Invalid entry, please try again.\n");
			break;
	}
}while (choice <= 5);

	return 0;
}
I and i are not the same thing, although when I quote the code it magically has fixed itself.

What do you do to cause the error, and how far do you get?
tabstop is offline   Reply With Quote
Old 07-08-2009, 04:44 PM   #7
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by tabstop View Post
I and i are not the same thing, although when I quote the code it magically has fixed itself.

What do you do to cause the error, and how far do you get?
I noticed that, in my code they're both "I" but when I post it hear they convert to "i"s for some reason. I get to the point where I add a new message, tell it how many messages I want to add, then as soon as I hit enter after inputing the first message it freezes.
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 04:57 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by CMakesMeSad :( View Post
I noticed that, in my code they're both "I" but when I post it hear they convert to "i"s for some reason. I get to the point where I add a new message, tell it how many messages I want to add, then as soon as I hit enter after inputing the first message it freezes.
That's not frozen. You said you wanted to input <n> messages; you've got to type <n> messages.
tabstop is offline   Reply With Quote
Old 07-08-2009, 04:57 PM   #9
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by CMakesMeSad :( View Post
Im here to learn so if you have better alternatives id love to hear them
fgets
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 05:38 PM   #10
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by tabstop View Post
That's not frozen. You said you wanted to input <n> messages; you've got to type <n> messages.
I removed everything and just put this:

Code:
printf("Enter priority:\n");
scanf("%d", &std[i].priority);
I still get a segmentation fault as soon as I enter a number
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 05:39 PM   #11
Registered User
 
Join Date: Jun 2009
Posts: 20
Quote:
Originally Posted by ಠ_ಠ View Post
fgets
Thanks, Ill definitely look into it!
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 05:46 PM   #12
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> I still get a segmentation fault as soon as I enter a number

You should post the current version of the code, otherwise we'd just be making wild guesses.
Sebastiani is offline   Reply With Quote
Old 07-08-2009, 05:48 PM   #13
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by CMakesMeSad :( View Post
I removed everything and just put this:

Code:
printf("Enter priority:\n");
scanf("%d", &std[i].priority);
I still get a segmentation fault as soon as I enter a number
If you removed the for loop then you never initialized I
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 05:51 PM   #14
Registered User
 
Join Date: Jun 2009
Posts: 20
sorry guys, heres what i have now

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct MSG {
	int priority;
	int destination;
	char *message[100];
	int length;
	int is_valid;
};


int main ()
{

	int *my_list, *temp;
	int list_size, choice, position= -1, i;
	list_size=10;
	my_list = malloc(list_size *sizeof(int));
	struct MSG *messages;
	messages = malloc(list_size *sizeof(struct MSG));
	
	//struct MSG std[100]={0};

do
{
	printf("1.) Quit\n");
	printf("2.) Transmit next message\n");
	printf("3.) List messages by priority\n");
	printf("4.) Summarize message list\n");
	printf("5.) Add new message\n");
	printf(":");
	scanf("%d", &choice);
	switch(choice)
	{
		case 1:
			return 0;
		case 2:
			//transmit next message
			break;
		case 3:
			//list messages by priority
			break;
		case 4:
			//summarize message list
			break;
		case 5:
			//add new message
			printf("Enter a priority:\n");
			scanf("%d", &messages[position].priority);
			break;
		default:
			printf("Invalid entry, please try again.\n");
			break;
	}
}while (choice <= 5);

	return 0;
}

Last edited by CMakesMeSad :(; 07-08-2009 at 05:59 PM.
CMakesMeSad :( is offline   Reply With Quote
Old 07-08-2009, 05:54 PM   #15
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
Quote:
Originally Posted by CMakesMeSad :( View Post
sorry guys, heres what i have now

Code:
position= -1;
printf("Enter a priority:\n");
scanf("%s", &messages[position].message);
... what are you doing
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
why is strncpy segfaulting here? Calef13 C Programming 3 12-29-2008 03:27 PM
Segfaulting Distance Program radiohead C Programming 2 01-09-2006 08:48 PM
Why is it segfaulting? XSquared C Programming 7 03-30-2004 06:52 AM
a segfaulting algorythm demonus C Programming 8 08-11-2003 08:06 AM


All times are GMT -6. The time now is 07:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22