I got this code and a strange behaviour while printing the id member variable of node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int id;
int visited;
// struct node *neighbors_[];
};
struct graph
{
struct node nodes[26];
int adjMat[26][26];
};
struct stack_item
{
struct node node;
struct stack_item *next_;
};
struct myStack
{
struct stack_item *anfang_;
};
void initGraph(struct graph *graph_);
void push(struct myStack *stack_, struct node node);
int main()
{
struct graph graph;
struct myStack stack;
char ausgabe[26]="";
initGraph(&graph);
//READ DATA
char line[200];
int firstTime=1,first;
first=0;
push(&stack,graph.nodes[first]);
printf("ID %i\n",stack.anfang_->node.id);
printf("ID %i\n",stack.anfang_->node.id);
//FINISHED DATA READING
//CALL DFS
//dfs(graph,stack,ausgabe);
}
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
stack_->anfang_=&item;
}
void initGraph(struct graph *graph_)
{
int i,j;
for(i=0; i<26; i++)
{
struct node node= {i,0};
graph_->nodes[i]=node;
for(j=0; j<26; j++)
{
graph_->adjMat[i][j]=0;
}
}
}
If i execute this, the first print command leads to 'ID 0',the second to 'ID 1980796117'. How can this value change by printing it? Could please anyone help me, i've got really no idea!
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
/* BAD! */
stack_->anfang_=&item;
}
item is a local variable which, when the push function returns, goes out of scope. Any existing pointers which refer to this object are now invalid, and dereferencing it results in undefined behavior.
You will need to dynamically allocate item (i.e., malloc) if you need it to persist once the function has returned.
Related
#include <stdio.h>
#include <stdlib.h>
struct node{
int sayi;
struct node* sonraki;
};
struct node* baslangic=NULL;
struct node* olustur(int sayi){
struct node* yenidugum=(struct node*)malloc(sizeof(struct node));
yenidugum->sayi=sayi;
yenidugum->sonraki=NULL;
return yenidugum;
}
void sonaekle(int sayi){
struct node* sonaeklenecek=olustur(sayi);
if(baslangic==NULL){
baslangic=sonaekle;
}
else
{
struct node* temp=baslangic;
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
}
}
void yazdir(){
struct node* temp=baslangic;
while(temp!=NULL){
printf("%d =>",temp->sayi);
temp=temp->sonraki;
}
}
int main()
{
int secim, sayi;
while(1){
printf("1-Sona eleman ekle.....\n");
printf("Yapmak istediginiz secimi yapin...\n");
scanf("%d",&secim);
switch(secim){
case 1:
printf("Hangi elemani ekleyeceksiniz..\n");
scanf("%d",&sayi);
sonaekle(sayi);
yazdir();
break;
}
}
return 0;
}
I am trying to do make linked list but when I run this code it gives output like this:-443987883 =>. What is my mistake. I can't find it. Thank you for your answers and giving your time.
There are typos.
Instead of
baslangic=sonaekle;
where you are assigning the function pointer of the type void ( * )( int ) to the pointer of the type struct node * you need to write
baslangic=sonaeklenecek;
Instead of this while loop
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
you need to write
while(temp->sonraki!=NULL){
temp=temp->sonraki;
}
temp->sonraki = sonaeklenecek;
I have quite interesting problem, I guess. I am trying to implement Stack in C. Here is my header and implementation file(I have only implemented Push yet):
my.h:
typedef struct {
char type[3];
int nrOfOpr;
int num;
} BizarreNumber_t;
struct stackNode {
BizarreNumber_t data;
struct stackNode *nextPtr;
};
// stack related
extern void push(struct stackNode *topPtr, BizarreNumber_t info);
my.c:
void push(struct stackNode *topPtr, BizarreNumber_t info){
struct stackNode *newTop = malloc(sizeof(struct stackNode));
struct stackNode oldTop = *topPtr;
newTop->data=info;
newTop->nextPtr=&oldTop;
*topPtr=*newTop;
// printf("topPtr->next->data: %s\n", topPtr->nextPtr->data.type);
//
// printf("oldTop->data: %s\n", oldTop.data.type);
// printf("newTop->data: %s\n", newTop->data.type);
// printf("topPtr->data: %s\n", topPtr->data.type);
}
Lastly This is my main.c:
int main(int argc, char const *argv[]) {
struct stackNode* stackHead=malloc(sizeof(struct stackNode));
BizarreNumber_t a={"sa",1,1};
BizarreNumber_t b={"as",2,2};
stackHead->data=a;
stackHead->nextPtr=NULL;
printf("%s\n", stackHead->data.type);
push(stackHead,b);
printf("%s\n", stackHead->nextPtr->data.type);//HERE!!!
return 0;
}
In main, the line that I wrote "HERE!!!" is not correctly giving true output. Actually it does not give anything. Interesting thing is, whis gives correct output:
printf("%c\n", stackHead->nextPtr->data.type[0]);
I tried to print out every character in string, Results say that String comes main fine. But I cannot see. Why is it so?
stackHead is local variable created in main() function. Whatever modification or changes done with stackHead in push() method won't affect in main() method as it just call by value.
Instead of this pass the address of stackHead to push() method as
push(&stackHead,b); /* pass the address of stackhead */
And change the definition of push() accordingly.
void push(struct stackNode **topPtr, BizarreNumber_t info){
struct stackNode *newTop = malloc(sizeof(struct stackNode));
newTop->data = info;
newTop->nextPtr = *topPtr; /*new node next make it to head node */
*topPtr=newTop; /*update the head node */
}
I'm a noob student trying to write a program that uses binary search tree to organize the workers of a company. My teacher told me if I want to be able to create a new instance of the Worker structure, i can use malloc with the structure, which will return pointer to a new struct every time it's used, then i can edit the details of that new struct from another function. But how can i do it? No matter what i do it gets so complicated and i can't do it. Here's the code i've been able to write this part of the code, just to test if i can create and edit a new structure.
The main thing i ask is, how can i edit the newly created structure?
#include<stdlib.h>
#include<stdio.h>
struct btnode
{
int value = 5;
struct btnode *l;
struct btnode *r;
};
int test(int *p)
{
printf("%d", &p->value);
}
int main()
{
int *asdf = (int *)malloc(sizeof(struct btnode));
test(asdf);
}
Here is a mod of your program which allocates memory for one struct, fills in values for its members, and calls test() to print one member.
#include <stdlib.h>
#include <stdio.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
};
void test(struct btnode *p)
{
printf("%d", p->value);
}
int main(void)
{
struct btnode *asdf = malloc(sizeof *asdf);
if(asdf != NULL) {
asdf->value = 5;
asdf->l = NULL;
asdf->r = NULL;
test(asdf);
free(asdf);
}
return 0;
}
There are a number of small changes to detail too, I leave you to spot the differences.
First of all there are some mistakes in the code.
1) You can not assign values in the structure.
2) When you are making a pointer for the structure you need pointer of the structure not of the int (does not matter what you want from the inside of the structure)
This is the modified code which runs perfactly
#include<stdio.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
};
int test(struct btnode *p)
{
printf("%d", p->value);
}
int main()
{
struct btnode *asdf = (struct btnode*)malloc(sizeof(struct btnode));
asdf->value = 5;
test(asdf);
}
I'm trying to code simple push function and everything is fine until run time, executed code crashes. Anyone can clarify the reason, please ?
#include<stdio.h>
#include<stdlib.h>
typedef struct list{
int order;
struct list *next;
}list;
void push(struct list **arg,int i);
int main()
{
struct list **ptr=NULL;
for(int i=0;i<10;++i){
push(ptr,i);
}
return 0;
}
void push(struct list **arg,int i){
struct list *temp;
temp= malloc(sizeof(list));
temp->order=i;
temp->next=*arg;
*arg=temp;
}
Write
list *ptr=NULL;
^^^
for(int i=0;i<10;++i){
push( &ptr,i);
^^^^
}
Otherwise if to declare like this
struct list **ptr=NULL;
then this dereferencing
*ptr
results in undefined behavior.
#include <stdio.h>
#include <stdlib.h>
struct node
{
char name[30];
int age;
struct node *next;
} *list_head,*neos;
main()
{
}
void add_node_to_the_list(char data1[],int data2)
{
neos=(struct node *)malloc(sizeof(struct node));
strcpy(neos->name,data1);
age=data2;
neos->next=list_head;
list_head=neos;
}
void display_list()
{
struct node *p;
p=list_head;
while(p!=NULL)
{
puts(p->name);
printf("%d\n",age);
p=p->next;
}
}
When I compile this code I get an error because I haven't declare the "age" variable, although I have done that inside the struct node, outside the main function. Why?
Here you go:
#include <stdio.h>
#include <stdlib.h>
struct node
{
char name[30];
int age;
struct node *next;
}*list_head,*neos;
main()
{
}
void add_node_to_the_list(char data1[],int data2)/*Ç óõíáñôçóç áõôç ðñïóèåôåé êïìâï óôç ëéóôá*/
{
neos=(struct node *)malloc(sizeof(struct node));
strcpy(neos->name,data1);
neos->age=data2; //the correction is made here
neos->next=list_head;
list_head=neos;
}
void display_list()
{
struct node *p;
p=list_head;
while(p!=NULL)
{
puts(p->name);
printf("%d\n",p->age); //and here
p=p->next;
}
}
Note that you have been trying to access a struct element without pointing it.
Replace age=data2; by neos->age = data2;
and replace printf("%d\n",age); by printf("%d\n", p->age);
There is no age variable, age is a member of struct node.
You have declared it in a structure so to access it you have to pass by your structure
neos->age
age is just an element in a structure.
So assuming we have a structure defined as the following:
struct PlaceHolder {
int int_element
char char_element
} * place_holder;
to access any of the elements you need to access the structure first and reference the element in that structure.
place_holder->int_element = 5;
place_holder->char_element = "a";
In your case you have both list_head and neos as global variables, so to access an element in any of them you have to do:
list_head->age = data2;
neos->age = data2;