![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 20
| segfaulting! 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 | |
| | #2 |
| Banned 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 | |
| | #3 |
| Registered User Join Date: Sep 2007
Posts: 343
| Your program is essentially doing this: Code: char *s;
scanf("%s", &s);
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);
|
| cas is offline | |
| | #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 | |
| | #5 |
| Registered User Join Date: Jun 2009
Posts: 20
| |
| CMakesMeSad :( is offline | |
| | #6 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
What do you do to cause the error, and how far do you get? | |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: Jun 2009
Posts: 20
| 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 | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #9 |
| Banned Join Date: Mar 2009
Posts: 533
| fgets
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
| | #10 |
| Registered User Join Date: Jun 2009
Posts: 20
| |
| CMakesMeSad :( is offline | |
| | #11 |
| Registered User Join Date: Jun 2009
Posts: 20
| |
| CMakesMeSad :( is offline | |
| | #12 |
| Guest Join Date: Aug 2001
Posts: 4,895
| >> 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 | |
| | #13 |
| Banned Join Date: Mar 2009
Posts: 533
| If you removed the for loop then you never initialized I
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
| | #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 | |
| | #15 |
| Banned Join Date: Mar 2009
Posts: 533
| ... what are you doing
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |