Checking an array for NULL in C++


 
Thread Tools Search this Thread
Top Forums Programming Checking an array for NULL in C++
# 1  
Old 08-02-2011
Checking an array for NULL in C++

Hello All,

I have this question that how to check for an array for NULL?

HTML Code:
For eg. 
 
#include<iostream.h>
using namespace std;
 
int main()
{
      char arr[10];
      if(arr == NULL)
      {
            cout<<"NULL";
       }
}
Thanks a lott in advance!!!
# 2  
Old 08-02-2011
Include <stdio.h> and that'll work actually. But why would you check if an array's null? It can't be null. If it was a pointer, it might end up as NULL through a mistake.

I might just not understand what you're trying to do, though.

Last edited by Corona688; 08-02-2011 at 01:07 PM..
# 3  
Old 08-02-2011
Actually ...wt. I had to check was that if the char pointer has a string initialized or it is not yet initialized.

But I am checking it by doing (strcmp(arr, "")== 0) instead of (arr== NULL), latter which is not correct for the reason you mentioned above!!!!!

Thanks though SmilieSmilie
# 4  
Old 08-02-2011
Quote:
Originally Posted by mind@work
Actually ...wt. I had to check was that if the char pointer has a string initialized or it is not yet initialized.
To check if a string is zero length is easy even without a function call.
Code:
if(str[0] == '\0')

But uninitialized doesn't mean zero length, uninitialized means undefined. That means empty only if you're lucky. If you're unlucky it could be full of garbage. Don't check if a string is uninitialized -- don't leave around uninitialized strings, period.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Issue in inserting null string in array

I am getting some values from a file and putting them in an array..but the null strings are not getting passed to the array. So during printing the elements ,the null string is not showing in the output. during array size calculation it is also excluding null.Please let me know how to do it. # cat... (2 Replies)
Discussion started by: millan
2 Replies

2. Shell Programming and Scripting

Finding null column value using array

hi, Am trying to find a solution for finding a null column value inside a loop using array. for eg: two three five From the above array myarray,myarray and myarray having null values. But when am trying to check that space using some condition its not working. for (( i=0;... (4 Replies)
Discussion started by: rogerben
4 Replies

3. Shell Programming and Scripting

Checking for null condition in a UNIX variable

i have this code for i in `cat sql_output.txt` do -- some script commands done sql_output.txt has 1 column with employee_ids If the sql_output.txt is null then the do loop should not execute. How can i implement this. for i in `cat sql_output.txt` If i is null or empty then ... (5 Replies)
Discussion started by: rafa_fed2
5 Replies

4. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

5. Shell Programming and Scripting

NULL checking

Hi I need to check the output of a string to null. For eg the output I get for $KIT is empty. So want to echo something when the output is empty. How can I do that? Thanks in advance (6 Replies)
Discussion started by: Ananthdoss
6 Replies

6. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

7. Programming

C++ segmentation fault while checking for null pointer

void disptree(node *ptr) { if ((ptr->left) !=NULL) disptree(ptr->left); cout<<"Position:"<<ptr->pos<<" Data:"<<ptr->data<<endl; if ((ptr->right)!=NULL; disptree(ptr->right); } i'm getting a segmentation fault at the red line. i cannot understand what's the problem.... (3 Replies)
Discussion started by: vijaymrt
3 Replies

8. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies

9. Shell Programming and Scripting

Checking for null value

Dear All, I'm glad that I've also joined in this forum. Good work fellas... :b: Keep going...! :) I am having a .csv file where I am reading the values with comma(,) as delimiter. After reading each and every values, I would like to check whether it is empty (say null or blank spaces). How... (4 Replies)
Discussion started by: ganapathi.t
4 Replies

10. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies
Login or Register to Ask a Question