Is it safe to NULL an array?

Started by
28 comments, last by rip-off 12 years, 4 months ago
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];

Aluthreney -- the King of sheep.

Advertisement

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 ?
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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.

I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?
Yes absolutely. E.g.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
What were you expecting this to mean?
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. :blink:
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.

Aluthreney -- the King of sheep.

Given those requirements, the first thing I would reach for is a vector.

Stephen M. Webb
Professional Free Software Developer


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...

Aluthreney -- the King of sheep.

This topic is closed to new replies.

Advertisement