Why am I getting a segmentation fault when using scanf("%lf", GPA) if I am using a pointer for GPA, and scanf("%lf", &GPA) if it is not a pointer? - c

void fillQueueWithStudents(Queue *q)
{
int counter = 1;
char answer = 'Y';
//this signifies the current variables that
//are being dealt with
char *currName;
double GPA;
student stud;
while((answer != 'N') || (answer != 'n'))
{
printf("Please enter the info for student %i \n",counter);
//Gets the name of the current student being entered
printf("Name: ");
scanf("%s", currName);
printf("\n");
//Gets the gpa of the current student being entered
printf("GPA: ");
scanf("%lf", &GPA);
//somewhere here is the problem
printf("\n");
//makes our student have the current variables
stud.name = currName;
stud.gpa = GPA;
//enqueues this student
enQueue(q,stud);
//increments to the next student
counter++;
//This sees if the user wants to enter another student
printf("Do you want to enter another student. Enter 'N' or 'n' for
no. \n");
scanf("%c", &answer);
printf("\n");
}
}
I printed hello before and after the scanf and they both printed. It does not let me enter a value for GPA. I am wondering if it is scanning the entire newline or something like that. I do not understand why it will not let me put in a value. I am guessing it is trying to put too much into the GPA variable.
Below is the complete program.
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
char *name;
float gpa;
}student;
//This struct will represent a node in the queue
typedef struct Node
{
student *stud;
struct Node *next;
}QNode;
//This struct will identify the queue
//It will store the front and rear values to make
//it easier to enqueue and dequeue
typedef struct
{
QNode *front, *rear;
}Queue;
/*
This creates a new node with some data k.
It then sets its next value to NULL.
Then it returns the new node
Note: This node is not yet known to the queue. I will
use a separate function to do that
*/
QNode* newNode (student *stud)
{
QNode *temp = (QNode*)malloc(sizeof(QNode*));
temp->stud->gpa = stud->gpa;
temp->stud->name = stud->name;
temp->next = NULL;
return temp;
}
//Creats a empty queue
Queue* createQueue()
{
Queue *q = (Queue*)malloc(sizeof(Queue*));
q->front = q->rear = NULL;
return q;
}
/*
Uses the newNode function to make a new node in our linked list.
Then it puts that node in the correct place in the queue. This
is at the back
*/
void enQueue(Queue *q, student stud)
{
QNode *temp = newNode(&stud);//this creats a new node
/*
Checks if queue is empty, and if it is sets
our new node temp to rear and front. Then it
ends the function
*/
if(q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
//sets the current rears next to our new node temp
q->rear->next = temp;
//this makes temp the new rear
q->rear = temp;
}
/*
This function serves/dequeues a node.
As always nodes are dequeued from the front of the
queue
*/
QNode *deQueue(Queue *q)
{
//if the queue is empty tell the user by
//returning NULL
if(q->rear == NULL)
{
return NULL;
}
/*
I will return front to the user in case they want
to use the data included in it. I have to save
front first to do that. So I save it hear
*/
QNode *temp = q->front;
//Now I make front equal to its next
q->front = q->front->next;
/*
This makes rear NULL if front is NULL
This means the list is empty and we have dequeued the last
node.
*/
if(q->front == NULL)
{
q->rear == NULL;
}
printf("The element dequeued was %s \n",temp->stud->name);
//This returns the node we have dequeued
return temp;
}
void printQueue(Queue *q)
{
//Check is the queue is empty and if it is stop the
//function
if(q->rear == NULL)
{
return;
}
/*
I will now print out each node out one by one.
To do this I create a QNode pointer and make it equal to the
front. I will then print all contents of front
and make temp equal to the next print and repeat this
till the end.
*/
QNode *temp = q->front;
while(temp != NULL)
{
int counter = 1;
printf("Student %i: Name: %s GPA: %f \n",
counter, temp->stud->name, temp->stud->gpa);
//increment the counter to display next student
counter++;
//Get the next node
temp = temp->next;
}
}
/*
This function keeps adding students to the queue until the
user enters 'N' that signifies they are done
*/
void fillQueueWithStudents(Queue *q)
{
int counter = 1;
char answer = 'Y';
//this signifies the current variables that
//are being dealt with
char *currName;
float GPA;
student stud;
while((answer != 'N') || (answer != 'n'))
{
printf("Please enter the info for student %i \n",counter);
//Gets the name of the current student being entered
printf("Name: ");
scanf("%s", currName);
printf("\n");
//Gets the gpa of the current student being entered
printf("GPA: ");
scanf("%f", &GPA);
printf("\n");
//makes our student have the current variables
stud.name = currName;
stud.gpa = GPA;
//enqueues this student
enQueue(q,stud);
//increments to the next student
counter++;
//This sees if the user wants to enter another student
printf("Do you want to enter another student. Enter 'N' or 'n' for no. \n");
scanf("%c", &answer);
printf("\n");
}
}
int main()
{
Queue *q = createQueue();
fillQueueWithStudents(q);
QNode *temp;
do {
temp = deQueue(q);
free(temp);
printQueue(q);
} while(temp != NULL);
return 0;
}

This:
scanf("%s", currName);
is undefined behavior, since currName is not initialized. Using scanf() with plain %s to read in a name is not a good idea. Use a buffer (!) and then tell scanf() the buffer size:
char currName[128];
if(scanf("%127s", currName) == 1)
{
}
... and check the return value, as shown.
Note that %s stops at the first whitespace character, so you won't be able to read a name containing spaces.

Related

How to use gets and puts with linked list using pointers

I've written a linked list program and want to take input with spaces but it's not working.It works fine when I simply use "scanf" with %s but since I want to take input with multiple spaces I tried using "gets" and "puts" I've also tried using scanf("%[^\n]*c"); but on the console it gives me random garbage value for scanf("%[^\n]*c"); and for "gets" it reads blank space,
now let me tell you guys some info about the code and how it works
createNode(); function basically just creates a new node to store in the list and returns the address of this newly created node to the insertend(); function where it aligns the new node at the end of the list and in start=t=newnode start is the head pointer which points to the very first node and t is used to traverse the list until t's value becomes NULL,As you could see in the else part of the insertend(); function we're using another pointer t and storing the value of start in it so that we can traverse the list without losing the the address of the first node which is originally kept in the start pointer.
here's the code ->
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
char first[20];
struct Node* next;
};
struct Node* start=NULL;
struct Node* t,*u;
int i=1;
struct Node* createNode() //this function creates a newnode everytime it's called
{
struct Node* create=(struct Node*)malloc(sizeof(struct Node));
return create;
}
int length() //to measure the length of the list.
{
int count = 0;
struct Node* temp;
temp=start;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}
void insertend() //to insert a node at the end of the list.
{
int l;
struct Node* newnode = createNode();
printf("Enter Name : ");
fgets(newnode->first,sizeof(newnode->first),stdin);
if(start==NULL)
{
start=t=newnode;
start->next=NULL;
}
else
{
t=start;
while(t->next!=NULL)
t=t->next;
t->next=newnode;
t=newnode;
t->next=NULL;
printf("%s successfully added to the list!",newnode->first);
}
l=length();
printf("The length of the list is %d",l);
}
void display() //to display the list
{
struct Node* dis;
dis=start;
if(start==NULL)
{
system("cls");
printf("No elements to display in the list");
}
else
{
system("cls");
for(int j=1;dis!=NULL;j++)
{
printf("%d.) %s\n",j,dis->first);
dis=dis->next;
}
}
}
int menu() //this is just a menu it returns the user input to the main function
{
int men;
printf("Please select a choice from the options below :-\n\n");
printf("1.) Add at the end of the list\n");
printf("2.) Display list\n");
printf("3.) exit\n");
printf(" Enter your choice : ");
scanf("%d",&men);
return men;
}
int main()
{
while(1)
{
system("cls");
switch(menu())
{
case 1 : insertend();
break;
case 2 : display();
break;
case 3: exit(0);
default : system("cls"); printf("Ivalid choice!Please select an appropriate option!");
fflush(stdin);
break;
}
getch();
}
return 0;
}
gets is not to be used, it has been removed from C standard due to it's lack of security.
If you want to know more read Why is the gets function so dangerous that it should not be used?
If you use [^\n] it should work, though it's also problematic since this specifier does not limit the lenght of the stream to be read only that it must stop when finding a newline character.
I suspect the problem might be in the container rather than in the reading, maybe uninitialized memory, If you provide the struct code it'll be easier to diagnose.
You can try:
fgets(newnode->first, sizeof(newnode->first), stdin)
There is a caveat:
If the inputed stream is larger than the container, the extra characters will remain in the input buffer, you might need to discard them.
EDIT:
So the main problem was the fact that through your code you have lingering characters in the buffer, in the particular case of your fgets input it would catch a '\n' left in the buffer, so it would read it before the inputed stream, leaving it, again, in the buffer.
I added a function to clean up buffer, note that fflush(stdin) leads to undefined behaviour so it's a bad option.
I also added a few small tweaks.
- Note that conio.h is windows specific as is system("cls") and getch()(ncurses.h in Linux systems) so I commented it for this sample.
Live sample here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct Node
{
char first[20];
struct Node *next;
};
struct Node *start = NULL;
struct Node *t, *u;
void clear_buf(){ //clear stdin buffer
int c;
while((c = fgetc(stdin)) != '\n' && c != EOF){}
}
struct Node *createNode() //this function creates a newnode everytime it's called
{
struct Node *create = malloc(sizeof(struct Node));
return create;
}
int length() //to measure the length of the list.
{
int count = 0;
struct Node *temp;
temp = start;
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
void insertend() //to insert a node at the end of the list.
{
int l;
struct Node *newnode = createNode();
printf("Enter Name : ");
clear_buf(); //clear buffer before input
fgets(newnode->first, sizeof(newnode->first), stdin);
newnode->first[strcspn(newnode->first, "\n")] = '\0'; //remove '\n' from char array
if (start == NULL)
{
start = t = newnode;
start->next = NULL;
printf("%s successfully added to the list!", newnode->first);
}
else
{
t = start;
while (t->next != NULL)
t = t->next;
t->next = newnode;
t = newnode;
t->next = NULL;
printf("%s successfully added to the list!", newnode->first);
}
l = length();
printf("The length of the list is %d", l);
}
void display() //to display the list
{
const struct Node *dis;
dis = start;
if (start == NULL)
{
system("cls");
printf("No elements to display in the list");
}
else
{
system("cls");
for (int j = 1; dis != NULL; j++)
{
printf("%d.) %s\n", j, dis->first);
dis = dis->next;
}
}
}
int menu() //this is just a menu it returns the user input to the main function
{
int men;
printf("\nPlease select a choice from the options below :-\n\n");
printf("1.) Add at the end of the list\n");
printf("2.) Display list\n");
printf("3.) exit\n");
printf(" Enter your choice : ");
scanf("%d", &men);
return men;
}
int main()
{
while (1)
{
system("cls");
switch (menu())
{
case 1:
insertend();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
system("cls");
printf("Ivalid choice!Please select an appropriate option!");
clear_buf();
break;
}
getch();
}
return 0;
}

scanf() running endlessly, program stops running on a statement

This is the code for BFT(breadth first traversal) for a connected graph.
I run the code then I successfully make the adjacency list and also successfully print the adjacency list but after it program stops at when I take input from user to start the BFS from that inputed node.
scanf() is running infinitely or other error I can't able to identify.
#include<stdio.h>
#include<stdlib.h>
typedef struct root //the adjacency list which have all the vertices
{
int info;
struct adjacent *adj;
struct root *next;
} root;
typedef struct adjacent //Linked list which store adjacent nodes of any nodes in adj list.
{
int info;
struct adjacent *adj;
} adjacent;
typedef struct node // to make queue to store nodes to be explored.
{
int info;
struct node *next;
} nodeQ;
void insert(nodeQ **ft,nodeQ **rr,int n) // insert func of Q
{
nodeQ * new=(nodeQ *)malloc(sizeof(nodeQ));
new->info = n;
new->next = NULL;
if(*ft == NULL)
{
*ft=new;
*rr=new;
}
else
{
(*rr)->next = new;
*rr = new;
}
}
int delete(nodeQ **ft,nodeQ **rr) //delete func of Q
{
int value=(*ft)->info;
nodeQ *temp=*ft;
*ft=(*ft)->next;
free(temp);
if(*ft==NULL)
*rr=NULL;
return value;
}
void BFS(int total_nodes,int node_tobe_explored,root *head,nodeQ **ft,nodeQ **rr)
{
printf("ff");
int * visited=(int *)malloc(sizeof(int)*total_nodes);
for(int i=0;i<total_nodes;i++)
visited[i]=0; //initialize all value in visited array with 0
printf("aa");
visited[node_tobe_explored] = 1;
printf("%d",node_tobe_explored);
while(1) // this loop iterates until all nodes are not explored.
{
root *t=head;
while(t->info != node_tobe_explored) // this find the node address(t) of the node_tobe_explored.
t=t->next;
printf("bb");
adjacent * adj_node = t->adj;
while(adj_node)
{
if(visited[adj_node->info] == 0) //if that adjacent node is not visited then also we visit it.
{
int adj_node_val = adj_node->info;
visited[adj_node_val] = 1;
insert(ft,rr,adj_node_val);
printf(", %d",adj_node_val);
}
}
printf("cc");
if(*rr==NULL) //if Q is empty, means all nodes are explored, so we return.
return;
else //otherwise explore first node present in Q
node_tobe_explored = delete(ft,rr);
}
}
int main()
{
char ch;
int no,tot_nodes,start;
nodeQ *front=NULL,*rear=NULL;
printf("enter the no. of nodes: ");
scanf("%d",&tot_nodes);
root *head = NULL;
no = tot_nodes;
while(no!=0)
{ //to make the main chain of adjacency list.
root *new=(root *)malloc(sizeof(root));
new->info = no;
new->adj = NULL;
new->next = head;
head = new;
no--;
}
root *temp = head;
while(temp!=NULL)
{ // to add the adjacent nodes to main chain.
printf("enter the nodes adjacent to %d:\n",temp->info);
do
{
int element;
printf(" enter node: ");
scanf("%d",&element);
adjacent *nw = (adjacent *)malloc(sizeof(adjacent));
nw->info = element;
nw->adj = temp->adj;
temp->adj = nw;
printf("more adjacent nodes y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');
temp=temp->next;
}
printf("display of the structur of the linked list formed:\n");
root * head1=head;
while(head1) // to display the formed adj. list.
{
printf("%d--",head1->info);
adjacent *t = head1->adj;
while(t)
{
printf("%d,",t->info);
t=t->adj;
}
printf("\n");
head1=head1->next;
}
do
{
printf("enter the node value from which you want to start BFS: ");
printf("before [enter image description here][1]");
int st;
scanf("%d",&st);
printf("after");
BFS(tot_nodes,st,head,&front,&rear); //calling BFS func.
printf("do you want to print more traversals y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');
}
Excerpt from OPs source code:
printf("do you want to print more traversals y/n: ");
ch=getchar();
ch=getchar();
}while(ch=='Y'||ch=='y');
The intention was probably to read a letter (y or n) and the \n which confirmed the input.
So, the 2nd ch=getchar(); (for ENTER) overrides the previously read letter. The following fix would change this:
printf("do you want to print more traversals y/n: ");
ch=getchar();
getchar(); /* ignore the returned value - it's just to consume '\n'. */
}while(ch=='Y'||ch=='y');
Btw. this is something which should have been uncovered by a step-wise debugging of the cited four lines...

Printing a linked List only prints most recent data entry in C

I'm printing a linked list after each addition to it. The problem is that it only prints the most recently added node. The user is supposed to enter a string which is used to make a node and then that node is added to the list. Here is the code:
int main() {
char userChoice = printMenu();
int setNumber;
while (userChoice != 'q') {
printf("set: ");
scanf("%d", &setNumber);
Node **nodeArray;
nodeArray = malloc(10 * sizeof(Node *));
int i;
for (i = 0; i < 10; i++) {
nodeArray[i] = malloc(sizeof(Node));
}
if (userChoice == 'a')
add(&nodeArray, setNumber);
else
printf("Please enter a valid menu option.");
//printf("%s\n", (nodeArray[setNumber]->next)->data);
userChoice = printMenu();
}
void add(Node ***nodeArray, int setNumber) {
char userString[5];
printf("Please enter some data: ");
scanf("%s", userString);
Node *head = *nodeArray[setNumber]; /* head pointer to first element of array (dummy) */
Node *newNode = malloc(sizeof(Node)); /* new node to be added to array */
strncpy(newNode->data, userString, sizeof(newNode->data)); /* copies string entered by the user to data field of new node */
newNode->next = NULL; /* initializes next field of new node to NULL */
while (head->next)
head = head->next; /* points head to next element in list */
head->next = newNode; /* adds element to list */
head = *nodeArray[setNumber]; /* points head back to start of list */
Node *tmp = head;
printf("List is: ");
while (tmp->next) {
printf("%s", tmp->data);
tmp = tmp->next;
}
}
Just as an example, when I enter "one", it prints out "one". Then when I add "two" it only prints out two instead of printing out "one two". What am I doing wrong?
This *nodeArray[setNumber] means *(nodeArray[setNumber]) but you seem to mean (*nodeArray)[setNumber]. Or better, don't pass &nodeArray to add(), just pass nodeArray. So:
add(nodeArray, setNumber);
...
void add(Node **nodeArray, int setNumber) {
...
Node *head = nodeArray[setNumber];
The problem is that a new block of memory is being created every time you enter into the loop. Cut and paste the code that creates the array outside of the loop and everything should work fine.

Linked List Appcrash

I was trying to do an example about linked list. First, I added the values to the variables and there was no problem. But when I tried to get values from user, the program crashed when entering midterm 2 grade. I tried other input functions but the result is same. Where is the problem?
#include <stdio.h>
struct student
{
char *name;
int m1,m2,final;
struct student* next;
};
main()
{
addStudent();
system("PAUSE");
}
addStudent()
{
struct student *node = NULL;
struct student *firstnode;
firstnode = (struct student *)malloc(sizeof(struct student));
node = firstnode;
printf("press 0 to exit \n");
while(1)
{
printf("Student name: ");
scanf("%s", node->name)
if(node->name == "0") break;
printf("Midterm 1: ");
scanf("%d", node->m1);
printf("Midterm 2: ");
scanf("%d", node->m2);
printf("Final: ");
scanf("%d", node->final);
node->next = (struct student *)malloc(sizeof(struct student));
node = node->next;
}
node->next = NULL;
node = firstnode;
while(node->next);
while(node->next != NULL)
{
printf("%s - ",node->name);
printf("%d ", node->m1);
printf("%d ", node->m2);
printf("%d ", node->final);
node = node->next;
}
system("PAUSE");
return 0;
}
Fix 1
Remove the line
while(node->next);
Reason: It will put you on an infinite loop in most cases and it is unnecessary.
Fix 2
Replace the loop
while(node->next != NULL) {
}
with
if (node->next != NULL) {
while (node->next->next != NULL) {
}
}
Reason: You are allocating one additional struct each time and keeping it empty for reading next time. So the Linked List will end before the next becomes NULL.
Fix 3
Replace following in struct
char *name;
with
char name[80];
Reason: Memory not being allocated.
Fix 4
Replace at all occurrences of scanf (except for name)
scanf("%d", node->m1);
with
scanf("%d", &node->m1);
Reason: scanf needs memory location of data to be read.
Good luck
Your code has multiple errors.
To start with, the first scanf("%s", node->name) is missing its terminating semicolon.
Next, your function signatures are sloppy. main() should be int main(void). addStudent() should be int addStudent(void). (Or, get rid of its return 0 and let it return void.) Since you don't pre-declare addStudent(), you should define it before main() so that main() can know about it.
The crash, though, is because you haven't allocated memory for node->name. You've allocated memory for a node, but that doesn't give you space to put the name.

Why does this C code print only the first & last node entered?

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *Name;
int grade;
int cost;
}Hotel; /*This is data element in each node*/
typedef struct hinfo
{
Hotel h;
struct hinfo *next;
}Hinfo; /*This is a single node*/
typedef struct
{
Hinfo *next; /*This is the head pointer of the linked list*/
}HotelHead;
void createHotel(HotelHead *h);
void DisplayHotel(HotelHead h);
int main()
{
HotelHead *list=(HotelHead *)malloc(sizeof(HotelHead));
list->next=NULL;
createHotel(list);
DisplayHotel(*list);
return(0);
}
void createHotel(HotelHead *h) /*This function creates the list of hotels*/
{
char ans='y';
while(ans=='y' || ans=='Y')
{
char *name=(char *)malloc(20*sizeof(char));
Hinfo *new=(Hinfo *)malloc(sizeof(Hinfo));
printf("\nEnter hotel name: ");
scanf("%[A-Za-z0-9 ]",name);
printf("\nEnter hotel grade & cost: ");
scanf("%d %d",&new->h.grade,&new->h.cost);
new->h.Name=name;
new->next=NULL;
if(h->next==NULL){h->next=new;}
else
{
Hinfo *current=h->next;
while(current->next!=NULL){current->next=current->next->next;}
current->next=new;
}
printf("\nEnter another hotel?(Y/N): ");
scanf("%s",&ans);
getchar(); /*dummy getchar to eat unwanted character*/
}
}
void DisplayHotel(HotelHead h) /*This function displays all hotels in the list*/
{
Hinfo *current=h.next;
printf("\nHotel list:\n");
while(current!=NULL)
{
printf("\n%s %d %d\n",current->h.Name,current->h.grade,current->h.cost);
current=current->next;
}
}
You want to move current while walking the list instead of changing the value of current->next. Change this:
while (current->next != NULL) {
current->next = current->next->next;
}
to this:
while (current->next != NULL) {
current = current->next;
}
That said, it would be better to move current while adding new nodes instead of walking the linked list from the start every time. For example (skeleton code):
Hinfo *current;
while (...) {
Hinfo *new = malloc(sizeof(Hinfo));
// initialize new node
if (current != NULL) {
current->next = new;
}
current = new;
// prompt to enter more nodes
}
The DisplayHotel function is OK! The problem is with createhotel function.
When you do:
while( current->next != NULL ){
current->next = current->next->next;
}
Here you are actually changing the list, removing an element.
Try doing:
while( current->next != NULL ){
current = current->next;
}
Also the best approach would be to always have a pointer to the last element of the list on the head, so you add new elements directly instead of always going thorough the entire list! (remember to update the head when you add a new element)
This is incorrect:
char *name=(char *)malloc(sizeof(20));
You are allocating (sizeof(int)) bytes, not 20 bytes.
No matter what else you're doing, this will cause problems.

Resources