malloc syntax

This is a discussion on malloc syntax within the C Programming forums, part of the General Programming Boards category; I am making a program that makes a binary search tree with the values from an array of ten integers. ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Question malloc syntax

    I am making a program that makes a binary search tree with the values from an array of ten integers. When I compile, though, the compiler tells me the line with the call to malloc() "makes initialization pointer from integer without cast." I was wondering what was wrong with the function because I copied it directly from a book.

    Code:
    #include<stdio.h>
    typedef struct node* link;
    
    struct node {
    	int key;
    	link l;
    	link r;
    };
    
    static link head, z;
    
    link NEW(int key, link l, link r) {
    	link x = malloc(sizeof *x);
    	x->key = key;
    	x->l = l;
    	x->r = r;
    	return x;
    }

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    #include <stdlib.h>
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    I'm an idiot. Thank you so much.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    No your not, Most people don't even know that malloc is in stdlib.h Alot of compilers automatically include it for you. You should think you are lucky to have such a strict compiler

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and before some plonker tells you to cast the return from malloc to hide the error, read this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    link x = malloc (sizeof *x); you are allocating space to something thats not a pointer...

    also you're allocating the size of a pointer, which is probably not what you want, this was in a book?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by nonpuz
    link x = malloc (sizeof *x); you are allocating space to something thats not a pointer...

    also you're allocating the size of a pointer, which is probably not what you want, this was in a book?
    link is a pointer, therefore the original code is correct.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    467
    Oh you're right....durrf

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Yeah, the link x = malloc(sizeof *x) threw me off, too. I heard that the book had some errors in their code, so I look at the programs to see if they are right or not instead of taking them for granted. Right now I'm having trouble with another set of functions for the program I am working on, and I am having trouble implementing a sorting algorithm for another program I'm writing, although I'm sure I'm just not implementing them right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21