Thread: Initalizing global variables to constants

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Initalizing global variables to constants

    So, I'm working under Visual Studio 6 and I'm coding in C (I have to use stricly ANSI C for this work assignment).

    I'm trying to create some global scope pointers and initailize them to constants and I keep getting the error C2099.

    Code:
    char* fileText = (char*)malloc(100);
    Error text: error C2099: initializer is not a constant

    It seems to me that '100' is about as constant as you can get. I tried making a #define and initializing to that, but that also failed. Is it just impossible to initalize dynamic memory in global scope in C? Because it works fine within a function.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    First of all, you shouldn't cast the return value of malloc().

    Secondly, it's not the value you're passing to malloc() that it's saying isn't constant; it's the return value of malloc() that it's complaining about. You can't use malloc() to initialize a global variable.

    Why are you using malloc() there anyway? Why not just char fileText[100];?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Don't you always need to cast the return value of malloc since it returns a void*?

    How is the return of malloc not constant in global scope but is constant in function scope? Is that just an idiosyncrisy of C?

    I suppose I could just make them static arrays, but I still want to know why I can't do what I was doing before

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Variables with function scope don't need constant initializers. Global variables do.
    Don't you always need to cast the return value of malloc since it returns a void*?
    Nope. That's a C++ idiom, but not C. As long as you're not compiling your program as C++ and you #include <stdlib.h> then you shouldn't get any warnings/errors if you omit the cast.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char* fileText = (char*)malloc(100);
    You're probably trying to do this outside the scope of any function.
    Unlike C++, all C executable code has to be inside a function.

    So
    Code:
    char* fileText;
    int main ( ) {
        fileText = malloc(100);
        // rest of code
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Salem
    > char* fileText = (char*)malloc(100);
    You're probably trying to do this outside the scope of any function.
    Unlike C++, all C executable code has to be inside a function.

    So
    Code:
    char* fileText;
    int main ( ) {
        fileText = malloc(100);
        // rest of code
    }
    Isn't that pretty much what I said?
    If you understand what you're doing, you're not learning anything.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Isn't that pretty much what I said?
    Yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables, or...
    By Vber in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 03:49 AM