Thread: C structure basic code not compiling

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    C structure basic code not compiling

    hello sirs, why is this code not working? it does nothing special just trying to understand.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    
    {
        int a;
        struct node *next;
    
    };
    struct node *a;
    a = (struct node *)malloc(sizeof(struct node));
    int main(){
    
    //d nothing
    
    }
    the error on the compiler logs(code blocks)

    are

    Code:
    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
    C:\Users\xx\Documents\strcts\struct_stack.c|12|warning: data definition has no type or storage class|
    C:\Users\xx\Documents\strcts\struct_stack.c|12|error: conflicting types for 'a'|
    C:\Users\xx\Documents\strcts\struct_stack.c|11|note: previous declaration of 'a' was here|
    C:\Users\xx\Documents\strcts\struct_stack.c|12|warning: initialization makes integer from pointer without a cast|
    C:\Users\xx\Documents\strcts\struct_stack.c|12|error: initializer element is not constant|
    ||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Because you have non-declarative code outside of a function. Put the statement with the malloc call in the body of the main function instead writing the comment "//d nothing" which is ridiculous when you actually want to do something. Actually, the declaration of the pointer named a should also go in the main function, and then you should remember to free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c structure: why error comes when compiling with gcc
    By emailtoalihasna in forum C Programming
    Replies: 2
    Last Post: 03-18-2014, 06:38 AM
  2. Problem with compiling this reallly basic code
    By iluvanimestyle in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2011, 05:50 PM
  3. Basic doubt in C structure
    By cbalu in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2009, 11:03 AM
  4. Basic compiling problem for a beginner
    By crazychile in forum C Programming
    Replies: 4
    Last Post: 09-21-2008, 02:27 AM
  5. Problems compiling basic program
    By thestrap in forum C Programming
    Replies: 15
    Last Post: 09-05-2006, 10:41 AM

Tags for this Thread