the compiler stops working ,pls give me some solution
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct node *next;
}node;
node * create()
{
node *p;
p=malloc(sizeof(node));
p->next=NULL;
return p;
}
void add(node **h,int ele)
{ node *temp;
temp=(*h);
node *p=create();
while(temp->next!=temp)
temp=temp->next;
temp->next=p;
p->next=temp;
p->data=ele;
(*h)=temp;
}
void main()
{ int ch,ele;
node *h;
h->next=h;
do
{ printf("\n1 add 2 delete 3 insert at kth position 4 delete at kth position");
scanf("%d",&ch);
if(ch==1)
{
scanf("%d",&ele);
add(&h,ele);
}
//if(ch==2)
}while(ch!=5);
}
Like I said in my comment, my guess is that you have a crash, that the program stops when you run the program, and that it builds without errors.
The reason is that you are using the local non-static variable h without initializing it. All local non-static variables have an indeterminate value unless initialized, and using those variable without initialization leads to undefined behavior.
The solution is very simple: Allocate memory for a node (either as a normal variable or dynamically) and initialize the pointer h with that.
Most compilers are actually capable of detecting problems of this kind, but since it's syntactically and semantically legal it will not issue an error, but a warning instead. And if you don't get such a warning then you need to enable more warnings.
Related
typedef struct node {
int data;
struct node *next;
} Node;
void checkEmpty(Node *list) {
printf(list == NULL ? "true" : "false");
}
The first time, I created the main() function, made a list directly in it, and called checkEmpty(). It printed true.
int main() {
Node *list;
checkEmpty(list); // it return true
return 0;
}
Then, I created a new function menu(), created a list inside it, and called checkEmpty(). It printed false.
void menu() {
Node *list;
checkEmpty(list);
}
int main() {
menu(); // it return false
return 0;
}
Why does this happen?
The declaration Node *list inside a function does not initialize list. Its value is indeterminate, meaning it does not have to behave as if it has any fixed value at all. The compiler may generate code that uses a different value for it each time it is used, such as reading from memory on one occasion or using a value from a processor register on another.
To fix your program, give list an initial value with Node *list = 0; or Node *list = NULL.
Also turn on compiler warnings and elevate warnings to errors. Check your compiler documentation for the switches to do that. Also pay attention to compiler warning and error messages.
You have a pointer of type Node that is currently not pointing to any memory specifically. However, there can already be junk data stored into the memory block of the pointer so that technically it's not pointing to NULL and it is pointing to something. The first time you created a pointer, could've been by chance that it's already NULL in the memory block.
So,if you do declare the pointer, it is always good practice to have the pointer point to NULL first.
Node *list = NULL;
I have created this structure and I am trying to perform the basic operations with the help of functions which I created. My program works but I have to declare the temp variable inside every function. I tried making it global but it says "illegal initialization".
struct node
{
int data;
struct node* next;
};
struct node* head=NULL;
struct node* temp=(struct node*)malloc(sizeof(struct node));
//If I remove the above line and move it to the disp function it works
//but in this case it says illegal initialization
void disp()
{
temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->data);
temp=temp->next;
}
}
Should I add the whole program to this code?
In C, the global variables are initialized by compiler and hence it must be a constant value like NULL in the first line. But in your case, you are trying to call a function (malloc()) which is not allowed.
Source: https://www.geeksforgeeks.org/initialization-global-static-variables-c/
Hi guys I'm learning C programming. I wanted to write some codes for learning linked list topic but there is a problem. This code about creating linked list with 5 nodes, writing something into 3rd node, printing them to console.
Here is all of my codes:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
struct node{
char *data;
struct node *next;
};
typedef struct node node;
node *root;
void nodeAdd(node *n, int x)
{
node *iter;
iter=root;
for(int i=0;i<x;i++)
{
iter->next = (node*)malloc(sizeof(node));
iter->next->data="0";
iter->next->next=NULL;
iter=iter->next;
printf("Node created.\n");
}
}
void nodeWrite(node *n,char *string,int x)
{
node *temp;
temp=root;
for(int k=0;k<x;k++)
{
temp=temp->next;
}
strcpy(temp->data,string); //HERE IS ERROR
printf("\n");
printf("%s \n",temp->data);
printf("Node writed.");
}
void nodePrint(node *n)
{
while(root->next!=NULL)
printf("%s\n",root->data);
root=root->next;
}
int main(int argc, const char * argv[])
{
root = (node*)malloc(sizeof(node));
nodeAdd(root,5);
nodeWrite(root,"WTF", 3);
nodePrint(root);
return 0;
}
data is an unintialized pointer variable. Initialize with the address of a valid memory that you allocate. That will solve the problem. Now you have udnefined behavior.
What you can possibly do is
Use char array instead of using pointer.
Allocate dynamically the memory.
In case of 1.
struct node{
char data[MAXSTRINGLEN];
struct node *next;
};
In case of 2:
Initially make the pointers point to NULL. So now you can allocate to it like this
temp->data = malloc(sizeof *temp->data*MAXSTRINGLEN);
if( temp->data == NULL)
{
fprintf(stderr,"Error in malloc");
exit(1);
}
Just one point, free the allocated memory when you are done working with it.
Use of the global variable here is not really required here. You can always pass return pointers from memory and assign it to the struct node*. Or yes you can use double pointers. Use of global variable is not needed here.
Clean up code, that are redundant and not required. That makes things readable and less confusing.
The program initially is designed incorrectly and has undefined behavior..
For example the data member data of the node root was not initialized. So its output in the function nodePrint results in undefined behavior. Moreover the function itself is incorrect.
Neither function uses its parameter node *n.
In this statement
strcpy(temp->data,string);
there is an attempt to change the string literal pointed to by the data member temp->data provided that the data member was initialized (as it was pointed above the data member is not initialized for the node root). You may not change a string literal. Any attempt to modify a string literal leads to undefined behavior.
There is no need to declare the node root as a global variable.
Parameters of the function main are not used in the program. So the function should be declared like
int main( void )
I was writing a program to implement BST and I declareed two structures for that.
struct node
{
int data;
struct node *link[2];
};
struct tree
{
struct node *root;
};
typedef struct node node;
typedef struct tree tree;
And then i wrote a function to insert the elements in it.
void insert_iter(tree *tree, int data);
But in my main() function, I got confused that what thing shoud I initialise as NULL. So, I did this in my main() function.
tree *tree=NULL;
And it gave an error of segmentation fault.
But then I realised that the first condition in my insert_iter() function calls for tree->root, so this error was bound to happen.
But then I am confused about how to initialize this. How to go on for this?
EDIT: I have updated with the required code!!
this is the main() function
int main()
{
tree *tree=NULL; // all the confusion is regarding this
printf("hello"); // for debugging
//*tree->root=NULL; // tried this one but it was wrong
int value,choice;
while(1)
{
printf("enter the element : ");
scanf("%d",&value);
insert_iter(tree,value);
printf("do you want to enter more : 0 or 1 : ");
scanf("%d",&choice);
if(choice==0)
break;
}
return 0;
}
and this is the function that i used to insert the elements
void insert_iter(tree *tree, int data)
{
if(tree->root==NULL)
tree->root=newNode(data); // function to make a new node
else{
node *it=tree->root;
int dir;
while(1)
{
dir=data>it->data;
if(it->link[dir]!=NULL)
it=it->link[dir];
else
{
it->link[dir]=newNode(data);
break;
}
}}}
tree *tree=NULL; // all the confusion is regarding this
// the above is setting a pointer to NULL, which is ok
*tree->root=NULL; // tried this one but it was wrong
// the above is setting a
// (not initialized to point to any specific memory)
// offset in the tree struct variable
// (that offset is not pointing any specific memory)
// to set the first 4 bytes of that memory to NULL
// which should/will cause a seg fault event due to undefined behaviour
// a workable solution might be:
tree *pTree = malloc( sizeof(tree) );
// and then this would work:
tree->root = NULL; // notice no '*' dereference needed
//I would also strongly suggest that the variable names be different
//than the struct names, and not just in the capitalization of the name.
//The use of different names will greatly enhance
//the readability and understandability and clarity of the code
//(and will avoid any misunderstandings by the compiler and future maintainer)
I have posted the link to my BST code on ideone: http://ideone.com/P7850n
In the main function I am getting an error when I read values in the while loop and insert into BST, but it works fine if I use a for loop. What could be the possible explanation for this error which occurs only with the while loop ?
#include <stdio.h>
#include <stdlib.h>
//data struct for BST node
typedef struct BST
{
int data;
struct BST *left;
struct BST *right;
}node;
//make node from given data
node* makeNode(int data)
{
node *n=(node*)malloc(sizeof(node));
n->data=data;
n->left=NULL;
n->right=NULL;
return n;
}
//insert node in BST
node* insert(node* root,int key)
{
if(root==NULL)
return makeNode(key);
if(key < root->data)
root->left=insert(root->left,key);
else
root->right=insert(root->right,key);
return root;
}
//inorder printing prints in sorted order
void inorder(node* root)
{
if(root==NULL)
return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
//driver function
int main(void) {
// your code goes here
node *root;
int s,i,key;
scanf("%d",&s);
while(s--)
//for(i=0;i<s;i++)
{
scanf("%d",&key);
root=insert(root,key);
}
inorder(root);
return 0;
}
Most probably this is an uninitialized variable root.
The compiler re-uses the same memory for variables, either declared in your program or used internally, after they are not anymore needed, so that other variables later occupy the same memory. In C (unlike, say, Perl), when memory is assigned to a variable, it is not automatically cleared: you should do it yourself, which is called initialization: typically as soon as you declare a variable, you should assign it some value: int year = 2014;. If you use a variable before you assign it a value, it's value will be whatever happens to be in memory that it occupies, left from other variables or even other running programs.
In your case, when you initialize the for loop with i=0, this 0 probably uses the memory later used for root, so accidentally it works. When you initialize the while loop with non-zero s, root uses memory that happens to be non-zero.
The solution is to initialize root = NULL;, and in general it's a good habit to always initialize all variables.
Without node *root = NULL; you are trying to access undefined memory address as root will contain any random data. So you can get valid behavior or any other behavior including crash.
As root is not initialized in inser() function if(root==NULL) may or may not be true and hence you will get different behavior.
This has nothing to do with for or while loop.
its always to initialize any memory variable to NULL or any other variable to 0,while writing any piece of code,otherwise you will always get any unpredictable crash or result.
like in this case,do like this below:
node *root;
int s,i,key;
to
node *root = NULL;
int s =0;
int i = 0;
int key= 0;