![]() |
| | #1 |
| Registered User Join Date: Jun 2004
Posts: 124
| Initalizing global variables to constants 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); 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 | |
| | #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 | |
| | #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 | |
| | #4 | |
| Gawking at stupidity Join Date: Jul 2004
Posts: 2,324
| Variables with function scope don't need constant initializers. Global variables do. Quote:
__________________ 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 | |
| | #5 |
| and the hat of Jobseeking 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
}
|
| Salem is offline | |
| | #6 | |
| Gawking at stupidity Join Date: Jul 2004
Posts: 2,324
| Quote:
__________________ 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 | |
| | #7 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,693
| > Isn't that pretty much what I said? Yes |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |