I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?
eg.: char array[NULL][NULL];
Is it safe to NULL an array?
I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?
eg.: char array[NULL][NULL];
What exactly would be the point of that ?
Null is just: #define NULL 0
So, you've actually written: [font="Courier New"]char array[0][0];[/font]
No, you can't have an array with a constant-sized dimension of zero.
So, you've actually written: [font="Courier New"]char array[0][0];[/font]
No, you can't have an array with a constant-sized dimension of zero.
Yes absolutely. E.g.
I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?
int array[3] = {1, NULL, 2};
What this does is fill the array with 1, 0, and 2.eg.: char array[NULL][NULL];[/quote]That's something completely different. You're not trying to use NULL as an array value any more like you stated. You're now trying to use NULL as the array dimenstions. The result being an array of zero size which is illegal and would only compile on compilers that allowed such a thing as a language extension.
An array of zero length would be like a room with zero width, length and height. Hard to imagine and certainly not useful for anything if it could exist.
I'm trying to make a text-based game to see if I can learn anything new about c++. I'm building a class "Map" that allows me to build an area where the player ("P") will be able to move through. The reason I'm creating a class is because I want to create different areas, so I had decided that in the class the "Map" would be char grid[NULL][NULL] (wouldn't exist) and when I wanted to create a new area for my game, I would just type in the values for my "Map" object.
I'm trying to make a text-based game to see if I can learn anything new about c++. I'm building a class "Map" that allows me to build an area where the player ("P") will be able to move through. The reason I'm creating a class is because I want to create different areas, so I had decided that in the class the "Map" would be char grid[NULL][NULL] (wouldn't exist) and when I wanted to create a new area for my game, I would just type in the values for my "Map" object.
I think what you are thinking of is actually char* grid = NULL, which is different from what you are doing here. To elaborate on what Hodgman said earlier, most of the time NULL is defined as 0, but in C it can be all sorts of crazy stuff. In C++ there are some other weird things specifically when dealing with pointers.
if ( ptr == 0 ) // this will check if ptr is a null pointer
if ( ptr == NULL ) // same
if ( !ptr ) //same
int blah = 0;
if ( ptr == blah ) //Can be very different depending on compiler optimizations to your code.
http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0
Similarly, especially useful for debugging, null pointers are not always 0 value. The reason ptr == 0 works is because the const literal 0 compared with pointers always treats the 0 as a null ptr, not necessarily corresponding to the memory address at 0.
Given those requirements, the first thing I would reach for is a vector.
But an array is a vector...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement