Weird Malloc Error

This is a discussion on Weird Malloc Error within the C Programming forums, part of the General Programming Boards category; Can someone tell me why malloc wont work in this case? (note, line 13 is the char line) My error ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Weird Malloc Error

    Can someone tell me why malloc wont work in this case?
    (note, line 13 is the char line)

    My error is:
    1>c:\documents and settings\user\my documents\visual studio 2008\projects\code\code\code.c(13) : error C2143: syntax error : missing ';' before 'type'
    Code:
    char * ptr;
    
    int main (int argc, char *argv[])
    {
    	ptr = malloc(9000);
    
    	char cmd[50] = "";
            return 0;
    }
    Last edited by someprogr; 12-27-2007 at 05:53 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    You need to provide a small but complete example of code that illustrates your problem. If there is a line 13 in your sample, that means you have removed other code which might be contributing to the problem. When an error message about a missing semi-colon occurs, the real cause can be code several lines before the compiler gets confused enough to complain.

    Assuming your code is C (not C++), and that you are not using a C++ compiler with C code, you need to #include <stdlib.h>

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    The compiler gives you the line where it concluded that there is an error, not where the error actually is. Errors like these usually spring from something not being quite right on the previous line. Perhaps you're missing a semicolon on line 12?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    ptr is global, therefore the initialization of ptr counts as code, not declaration. You can't have declarations following code. You must call malloc() after declaring char cmd[]

  5. #5
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by brewbuck View Post
    ptr is global, therefore the initialization of ptr counts as code, not declaration. You can't have declarations following code. You must call malloc() after declaring char cmd[]
    Accommodating to a lower denominator is good practice, for portability reasons, since C89 forbids the mixing of declaration and code. However is fine if you are compiling with C99.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Best just put that char* ptr into main instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM

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