By Mystheman

2020-08-01 13:07:52 8 Comments

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));
    printf("Enter the value of 'ara' element");
    head = siraliEkle(10,head);
    head = siraliEkle(5,head);

void Print(struct Node *root){
        root = root->next;

struct Node *siraliEkle(int sayi, struct Node *root){
        root = (struct Node*) malloc(sizeof(struct Node));
        root->x = sayi;
        root->next = NULL;
        return root;
    else if(root->next==NULL){
            struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
            temp->x = sayi;
            temp->next = root;
            root = temp;
            return root;


@Greg Price 2020-08-01 14:34:08

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;

@MikeCAT 2020-08-01 13:12:33

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).

@bruno 2020-08-01 13:16:30

may be also warn the OP about the very strange definition of Print ? while(root != NULL) { printf("%d\n",root->x); root = root->next; } seems more appropriate

@Mystheman 2020-08-01 13:18:12

Thank you but I need to ask something. Can't we implement functions below the main function? I used to use my functions in that way when I created a little project with one of my friends and didn't have any error like this?

@bruno 2020-08-01 13:20:00

@Mystheman of course you can move them before main to avoid to declare them

@Mystheman 2020-08-01 13:21:33

Ohh okay, I've just remembered, we used to define our functions with their types without any parameters before the main function. Like this: 'struct Node* siraliEkle(); void main()'

@bruno 2020-08-01 13:23:49

@Mystheman give the full profile when you declare them, the more you say the more the compiler can help you producing warning/error. The compiler does not produce warning/error against you but for you

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] How do you set, clear, and toggle a single bit?

17 Answered Questions

[SOLVED] How do I use extern to share variables between source files?

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 806385 View
  • 1252 Score
  • 11 Answer
  • Tags:   c function-pointers

10 Answered Questions

[SOLVED] Pointers to string in functions

2 Answered Questions

[SOLVED] malloc and pointer in a struct

  • 2013-05-14 18:19:53
  • u123
  • 2313 View
  • 2 Score
  • 2 Answer
  • Tags:   c pointers

2 Answered Questions

[SOLVED] pointer to array of struct

  • 2012-12-20 02:51:45
  • user1832809
  • 411 View
  • 2 Score
  • 2 Answer
  • Tags:   c linux malloc

2 Answered Questions

[SOLVED] Realloc is not resizing array of pointers

  • 2012-02-28 07:33:35
  • user994165
  • 2425 View
  • 2 Score
  • 2 Answer
  • Tags:   c

3 Answered Questions

[SOLVED] copy elements of an array to a linked list in C

  • 2011-12-05 11:43:27
  • user974227
  • 6987 View
  • 0 Score
  • 3 Answer
  • Tags:   c arrays structure

Sponsored Content