C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2006, 08:54 AM   #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.
maxhavoc is offline   Reply With Quote
Old 06-21-2006, 09:10 AM   #2
Gawking at stupidity
 
Join Date: Jul 2004
Posts: 2,324
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.

Ignore any "advice" esbo tries to give you. It's wrong.
itsme86 is offline   Reply With Quote
Old 06-21-2006, 09:13 AM   #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
maxhavoc is offline   Reply With Quote
Old 06-21-2006, 09:17 AM   #4
Gawking at stupidity
 
Join Date: Jul 2004
Posts: 2,324
Variables with function scope don't need constant initializers. Global variables do.
Quote:
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.

Ignore any "advice" esbo tries to give you. It's wrong.
itsme86 is offline   Reply With Quote
Old 06-21-2006, 11:17 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,693
> 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.

Salem is offline   Reply With Quote
Old 06-21-2006, 11:21 AM   #6
Gawking at stupidity
 
Join Date: Jul 2004
Posts: 2,324
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.

Ignore any "advice" esbo tries to give you. It's wrong.
itsme86 is offline   Reply With Quote
Old 06-21-2006, 11:25 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,693
> 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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
No error from multiple definitions of global variables dwks C Programming 5 08-06-2008 09:59 AM
Global variables used in a linked class, getting errors. RealityFusion C++ Programming 3 09-24-2005 12:25 AM
global variables - okay sometimes...? MadHatter C++ Programming 21 01-21-2003 04:23 PM
global variables rdnjr Linux Programming 0 01-07-2003 10:28 AM
Global variables, or... Vber C Programming 4 01-03-2003 03:49 AM


All times are GMT -6. The time now is 10:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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