#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;
Related
I have a char pointer in the structure to store names.When I inserted the values and printed the last value of name is getting printed for all nodes.
typedef struct tests{
int id;
char *p;
struct tests *next;
}test;'
'void add(test **root,int id,char *name){
test *newnode=(test* )malloc(sizeof(test));
newnode->id=id;
newnode->p=name;
newnode->next=NULL;
test *curr;
curr=(*root);
if((*root)==NULL){
(*root)=newnode;
}
else{
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=newnode;
}
}
Most likely what you intend to achieve is something along these lines:
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
typedef struct test_s {
int id;
char *p;
struct test_s *next;
} test_t;
test_t** add(test_t **root, int id, char const *name){
size_t len = strlen(name);
test_t *newnode=(test_t*)malloc(sizeof(test_t)+len+1); // 1 goes for \0 terminator
newnode->id=id;
strcpy(newnode->p=(void*)(newnode+1), name);
newnode->next=NULL;
test_t **tail = root;
while (*tail) {
tail = &(*tail)->next;
}
*tail = newnode;
return &(newnode->next);
}
Problem with your code is, you never copied your string assigning just a single pointer instead.
Take a look at pointers mechanics once again, additionally I would advise you to check string.h reference.
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 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.
I have the following two structs:
typedef struct label{
int id;
double p,*t,q,c;
int V[45];
struct label *next;
struct label *prev;
struct path *tail;
struct path *head;
}label;
typedef struct path{
int i;
struct path *Pperv;
struct path *Pnext;
}path;
void main (){
int i,j;
struct label *Current,*Head,*Tail;
struct path *test1,*path_head,*path_tail;
Head=(struct label*)malloc(1*sizeof(struct label));
Tail=(struct label*)malloc(1*sizeof(struct label));
Head->next=Tail;
Tail->prev=Head;
for (i=0;i<250000;i++)
{
Current=(struct label*)malloc(1*sizeof(struct label));
Current->t=(double*)malloc(15*sizeof(double));
Current->head=(struct path*)malloc(1*sizeof(struct path));
Current->tail=(struct path*)malloc(1*sizeof(struct path));
Current->head->Pnext=Current->tail;
Current->tail->Pperv=Current->head;
for (j=0;j<15;j++)
{
test1=(struct path*)malloc(1*sizeof(struct path));
test1->Pperv=Current->head;
test1->Pnext=Current->head->Pnext;
Current->head->Pnext->Pperv=test1;
Current->head->Pnext=test1;
test1->i=1;
Current->t[j]=23123.4323334;
}
Current->next=Tail;
Current->prev=Tail->prev;
Tail->prev->next=Current;
Tail->prev=Current;
Current->p=54545.323241321;
}
}
I just used an example of filling some of the variables in them so that I can make my question. What I am facing problem with is how to free the struct "Path" that is contained into the the first struct called name "Label".
I would me sth more than greatful if somenone could give me the code of how to correctly free both structs in C.
In general, you just need to be symmetrical with the calls to malloc/calloc:
label *current = malloc(sizeof(*current));
current->head = malloc(sizeof(*current->head));
...
free(current->head);
free(current);
I am trying to create a linked list in my program and I am not able to allocate memory to the structure pointer using malloc(). How do I allocate memory to variables in GCC? The sample program is given below. How to make it work in gcc?
#include<stdio.h>
#include <alloc.h>
struct node
{
int data;
struct node * link;
};
void insert (struct node *p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(p==NULL)
{
p=temp;
}
else{
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(p,7);
insert(p,9);
disp(p);
}
The error I'm encountering is:
Line 18: error: alloc.h: No such file or directory
In function 'insert':
Line 13: warning: incompatible implicit declaration of built-in function 'malloc'
malloc is in <stdlib.h>. Include that.
Reading the man page for that function would have given you that information. It's not compiler-dependent.
malloc is declared in <stdlib.h>, so that's what you want to #include.
The definition of malloc is in the stdlib.h file:
#include <stdlib.h>
instead of alloc.h.
Like the others say: your error occurs because you have to include stdlib.h instead of alloc.h
To get your list printed, you have to modify p in insert. Currently, you're passing NULL every time you call insert. Change your code that way (pass a pointer-to-pointer to insert):
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * link;
};
/* note **p instead of *p */
void insert (struct node **p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(*p==NULL)
{
*p=temp;
}
else{
while((*p)->link!=NULL)
*p=(*p)->link;
(*p)->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(&p,7);
insert(&p,9);
disp(p);
}
and it will print
7
9