Thread: error while allocating memory

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    error while allocating memory

    Hi !

    I am getting problem while allocating memory through malloc.Can any body explain me reason

    and solution regarding this.

    The code is as follow:

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    struct NODE
    {
        struct NODE* next;
       int data;
    };
    
    
    void insert(struct NODE* p,int value)
    {
     if(p==NULL)
    { 
      struct NODE* q=(struct NODE *)malloc(sizeof(struct NODE)); // ERROR 
      p=q;
     q->next=NULL;
    }
    else
    {
     while(p->next!=NULL)
          {
            p=p->next;
         }
     ////code 
     
    }
    }
    
    void main()
    {
      struct NODE* ptr;
     ptr=NULL;
    
     int choice,i,num;
    printf("\n Enter no. of items");
    scanf("%d",&choice);
    for(i=0;i<choice;i++)
    {
      scanf("%d",&num);
       insert(ptr,num);
    }
    }
    The compiler gives following in line above ERROR:

    syntax error: missing ' ; ' before 'type'

    Can any explain me what is this error and ho to remove it.

    thanks

    bargi
    Last edited by Bargi; 01-23-2007 at 11:10 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What is the whole code, #includes and all?

    If this is the whole code, you are missing these:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    Visit the FAQ for issues such as void main(), how to read a number from the command line, casting the return value of malloc, and other issues for the newcomer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    header files are properly included

    Hi ..
    header what you have mentioned is already included.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Okay, don't edit posts like that -- it makes a thread hard to follow.

    Copy and paste the whole thing always -- hopefully we've got the figured out.

    Copying and pasting your "whole" code as of the latest edit, the only issue I get is with using void main(), so you are apparently a glutton for punishment.

    So... what compiler are you using? And could you copy and paste the actual error message?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    error is following

    hi,

    the error is:

    Code:
    syntax error: missing ' ; ' before 'type'  //at ERROR which is mentioned in code
    I am using VS 6.0

    thanks
    bargi

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Odd... I get this:
    Code:
    H:\Projects\test.c(34) : error C2143: syntax error : missing ';' before 'type'
    H:\Projects\test.c(36) : error C2065: 'choice' : undeclared identifier
    H:\Projects\test.c(37) : error C2065: 'i' : undeclared identifier
    H:\Projects\test.c(39) : error C2065: 'num' : undeclared identifier
    Which tells me that this line is the problem:
    Code:
       struct NODE* ptr;
       ptr=NULL;
    
       int choice,i,num;
    Which means that this statement should not precede variable declarations.

    Better luck with this?
    Code:
       struct NODE* ptr=NULL;
    
       int choice,i,num;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    thanks,but can you explain me why this occur

    Hi dave!

    thanks a lot for your sol. I have solved my problem...

    but can you please explain me why this error occurs.

    Thanks

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For C90, you can't have statements after declarations/initializations.

    Here you have a declaration, a statement, and more declarations.
    Code:
       struct NODE* ptr;
       ptr=NULL;
    
       int choice,i,num;
    Ensuring that only declarations/initializations occur before statements "fixes" it.
    Code:
       struct NODE* ptr=NULL;
    
       int choice,i,num;
    The compiler reads it differently (and after a long time, you may too).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Dave_Sinkula
    For C90, you can't have statements after declarations/initializations.
    Dave???? Have you gone mad??? I sure hope that one can have statements _AFTER_ declarations. . . it'd be quite odd to have something like:
    Code:
    for (i = 0; i < z; i++){
        k = i*q+x;
    }
    
    int i, z, k, q, x;
    That would just be insane. . . and a bit hard to follow. You being a bit lysdexic tonight?

    EDIT: BTW, to the OP, Dave actually means that for C90 one cannot have declarations after statements.
    Last edited by Kennedy; 01-24-2007 at 12:41 AM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Kennedy
    Dave???? Have you gone mad???

    That would just be insane. . . and a bit hard to follow. You being a bit lysdexic tonight?
    Yes.

    [At least no one caught my issue with left/right earlier.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. allocating memory in constructor
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 07:45 AM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM