Stack Overflow Asked by Mystheman on August 9, 2020
I was working on data structures and while I was writing some code, I needed to return the address of the pointer that was defined in the structure. So here’s my code but when I compile and run it, it doesn’t work and give an error message as " assignment makes pointer from integer without a cast ". How should I rewrite it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
int x;
struct Node *next;
};
void main(){
int i;
struct Node *head;
head = (struct Node*) malloc(sizeof(struct Node));
head->next=NULL;
/*Ekle(5,head);
Ekle(10,head);
Ekle(15,head);
printf("Enter the value of 'ara' element");
scanf("%d",&i);
Print(head);
ArayaEkle(i,20,head);
Print(head);*/
head = siraliEkle(10,head);
head = siraliEkle(5,head);
Print(head);
}
void Print(struct Node *root){
while(root->next!=NULL){
root = root->next;
printf("%dn",root->x);
}
}
struct Node *siraliEkle(int sayi, struct Node *root){
if(root==NULL){
root = (struct Node*) malloc(sizeof(struct Node));
root->x = sayi;
root->next = NULL;
return root;
}
else if(root->next==NULL){
if(root->x>sayi){
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->x = sayi;
temp->next = root;
root = temp;
return root;
}
}
}
If you want to return the address for the struct pointer use the declaration
struct Node *siraliEkle(int sayi, struct Node *root);
As below and return:
return &root;
Answered by Greg Price on August 9, 2020
You must declare (or define) functions before using them.
Without declaration nor definition, types of function arguments are assumed to int
and it will cause trouble when actual types are not int
.
struct Node{
int x;
struct Node *next;
};
/* add these declarations */
void Print(struct Node *root);
struct Node *siraliEkle(int sayi, struct Node *root);
int main(){ /* also return type should be standard int */
Also don't forget to return something from siraliEkle
even if root != NULL && (root->next != NULL || root->x <= sayi)
.
Answered by MikeCAT on August 9, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP