int array problem
-
Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa
Not sure I understand your problem, but the simplest thing to do is to initialize your array to zeros before you even use it. But why use an array of size 10 when a size of 4 would suit your needs??
John P.
-
Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa
ints are just integer values. If you need to indicate an uninitialized int then you'll need to set aside one number to represent an invalid value. What are the valid range of values for your arry members? Pick a number outside that range :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
ints are just integer values. If you need to indicate an uninitialized int then you'll need to set aside one number to represent an invalid value. What are the valid range of values for your arry members? Pick a number outside that range :) Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Alternatively, you can use
int
pointers instead of array. This way you can check if the value isNULL
-
Alternatively, you can use
int
pointers instead of array. This way you can check if the value isNULL
-
Alternatively, you can use
int
pointers instead of array. This way you can check if the value isNULL
Sure that'll work. Another alternative - make a "smart int" class, wrapping an int with some flag indicating its initialized status :) Then you wouldn't need the added indirection of a pointer. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
:-D To make sure, use an array of int pointer pointers, each member pointing to the corresponding member in the int pointer array, whose members point to the actual ints!
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa
-
Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa
You need to use a different value and initialize the array to that. Since you want to allow 0 to be placed in the array then use -1 to initialize it. That is fill the array with -1 and check for that. If that is not an option, and there is no other value you can use, then you have a real problem.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra