Newbie question: comparison between pointer and integer

S

sieg1974

Hi,

when I compile the following code with gcc, I get a warning message
complaining about the comparison between pointer and integer at the
lines market.
How can I correct this problem? And why the compiler doesn't complain
at while( opendir( newSubDirectory ) )?

Thanks,

Andre

int makeSubDir( const char * theDirectory, char * newSubDirectory )
{
int subDir = 0;

*****-> if( opendir( theDirectory ) == NULL )
return( -1 );

do
{
subDir++;
sprintf( newSubDirectory, "%s/%03d", theDirectory, subDir );
}
while( opendir( newSubDirectory ) );<--why I don't get any warning
here?

mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR );
*****-> if( opendir( newSubDirectory ) == NULL )
return( -1 );

return( 0 );
}
 
A

Artie Gold

sieg1974 said:
Hi,

when I compile the following code with gcc, I get a warning message
complaining about the comparison between pointer and integer at the
lines market.
How can I correct this problem? And why the compiler doesn't complain
at while( opendir( newSubDirectory ) )?

Obviously, the there's no prototype for `opendir' (which is not a
standard C function) in scope. Such functions are assumed to return
`int'.
int makeSubDir( const char * theDirectory, char * newSubDirectory )
{
int subDir = 0;

*****-> if( opendir( theDirectory ) == NULL )

So here, you get a warning about the comparison.
return( -1 );

do
{
subDir++;
sprintf( newSubDirectory, "%s/%03d", theDirectory, subDir );
}
while( opendir( newSubDirectory ) );<--why I don't get any warning
here?

Because here, an int is fine.
mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR );
*****-> if( opendir( newSubDirectory ) == NULL )

And here, you assumedly get the same warning as above.
return( -1 );

return( 0 );
}

Turn up your warning levels as high as they'll go (you'll have to
look up how in your compiler's documentation) -- that'll help you out.

HTH,
--ag
 
I

Irrwahn Grausewitz

Hi,

when I compile the following code with gcc, I get a warning message
complaining about the comparison between pointer and integer at the
lines market.
How can I correct this problem? And why the compiler doesn't complain
at while( opendir( newSubDirectory ) )?

Thanks,

Andre

int makeSubDir( const char * theDirectory, char * newSubDirectory )
{
int subDir = 0;

*****-> if( opendir( theDirectory ) == NULL )

opendir() is a non-standard function. Based upon the mentioned warning
message I can only assume that it returns an integer which you compare
to a NULL-pointer constant. It /should/ work if you write:

if( opendir( theDirectory ) == 0 )

Generally you should post code that makes use of non-standard extensions
to the C language to an OS-/implementation-specific newsgroup and not to
comp.lang.c.
return( -1 );

do
{
subDir++;
sprintf( newSubDirectory, "%s/%03d", theDirectory, subDir );
}
while( opendir( newSubDirectory ) );<--why I don't get any warning
here?

See above.
mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR );
*****-> if( opendir( newSubDirectory ) == NULL )

See above.
return( -1 );

return( 0 );
}

Regards

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
 
A

Alf P. Steinbach

when I compile the following code with gcc, I get a warning message
complaining about the comparison between pointer and integer at the
lines market.

[Said lines comparing a function result with 'NULL']
How can I correct this problem?

Most probably by compiling as C++ instead of as C. In C++ 'NULL'
is required to be "0". Comparision of pointer equality with 0 works.

Second, are you sure a definition of 'NULL' is in scope?


And why the compiler doesn't complain at while( opendir( newSubDirectory ) )?

Any scalar value can be used where a boolean is expected.

int makeSubDir( const char * theDirectory, char * newSubDirectory )

In C++, consider using 'bool' as a return type, since judging from the
code the intended result is a boolean value.

In C++, consider using 'std::string' instead of character pointers.

It can save you a lot of time searching for bugs...


{
int subDir = 0;

Purpose of this?

Consider using self-explanatory names.

if( opendir( theDirectory ) == NULL )

Consider using the '!' operator.


return( -1 );

Parenthesis not necessary.

do
{
subDir++;

As a general convention, always use prefix '++' except where postfix is
absolutely necessary. You'll find that the postfix form is very seldom
necessary. Prefix is more general and can be more efficient.


sprintf( newSubDirectory, "%s/%03d", theDirectory, subDir );

Using 'sprintf' this way is _very_ dangerous, you risk a buffer overrun.

Consider using 'std::eek:stringstream'.


}
while( opendir( newSubDirectory ) );<--why I don't get any warning here?

Here you have either an infinite loop or a meaningless loop.


mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR );
if( opendir( newSubDirectory ) == NULL )

Again, consider using the '!' operator.

return( -1 );

Parenthesis not necessary.

return( 0 );

Parenthesis not necessary.
 
A

Alf P. Steinbach

[SORRY, WRONG GROUP. I THOUGHT IT WAS IN COMP.LANG.C++. OOPS.]


when I compile the following code with gcc, I get a warning message
complaining about the comparison between pointer and integer at the
lines market.

[Said lines comparing a function result with 'NULL']
How can I correct this problem?

Most probably by compiling as C++ instead of as C. In C++ 'NULL'
is required to be "0". Comparision of pointer equality with 0 works.

Second, are you sure a definition of 'NULL' is in scope?


And why the compiler doesn't complain at while( opendir( newSubDirectory ) )?

Any scalar value can be used where a boolean is expected.

int makeSubDir( const char * theDirectory, char * newSubDirectory )

In C++, consider using 'bool' as a return type, since judging from the
code the intended result is a boolean value.

In C++, consider using 'std::string' instead of character pointers.

It can save you a lot of time searching for bugs...


{
int subDir = 0;

Purpose of this?

Consider using self-explanatory names.

if( opendir( theDirectory ) == NULL )

Consider using the '!' operator.


return( -1 );

Parenthesis not necessary.

do
{
subDir++;

As a general convention, always use prefix '++' except where postfix is
absolutely necessary. You'll find that the postfix form is very seldom
necessary. Prefix is more general and can be more efficient.


sprintf( newSubDirectory, "%s/%03d", theDirectory, subDir );

Using 'sprintf' this way is _very_ dangerous, you risk a buffer overrun.

Consider using 'std::eek:stringstream'.


}
while( opendir( newSubDirectory ) );<--why I don't get any warning here?

Here you have either an infinite loop or a meaningless loop.


mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR );
if( opendir( newSubDirectory ) == NULL )

Again, consider using the '!' operator.

return( -1 );

Parenthesis not necessary.

return( 0 );

Parenthesis not necessary.

 
D

Derk Gwen

# How can I correct this problem? And why the compiler doesn't complain
# at while( opendir( newSubDirectory ) )?

Did you #include a header file that declare the function and that is,
presumably, some kind of pointer?

Did you #include a header file to ensure NULL was defined?
 
S

sieg1974

I guess I should have posted it at comp.os.linux group, sorry :)
Anyway, looking at the man page of opendir ( I copied it bellow ), it
should return a DIR pointer or NULL if it fails.
So, why can't I compare with NULL?

Thanks,

Andre

NAME
opendir - open a directory

SYNOPSIS
#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);

DESCRIPTION
The opendir() function opens a directory stream
corresponding to the
directory name, and returns a pointer to the directory
stream. The
stream is positioned at the first entry in the directory.

RETURN VALUE
The opendir() function returns a pointer to the directory
stream or
NULL if an error occurred.
 
S

Simon Biber

sieg1974 said:
I guess I should have posted it at comp.os.linux group, sorry :)

Yes, you should have. It's off-topic here in comp.lang.c.
Anyway, looking at the man page of opendir ( I copied it bellow ), it
should return a DIR pointer or NULL if it fails.
So, why can't I compare with NULL? [...]
SYNOPSIS
#include <sys/types.h>
#include <dirent.h>

Perhaps you forgot to include these headers?
 
J

Joona I Palaste

sieg1974 said:
I guess I should have posted it at comp.os.linux group, sorry :)
Anyway, looking at the man page of opendir ( I copied it bellow ), it
should return a DIR pointer or NULL if it fails.
So, why can't I compare with NULL?

You were already told this. Because you didn't provide a prototype for
opendir(), the compiler thought opendir() returned int, and complained
when it saw you comparing an int to NULL.
Try putting #include <dirent.h> at the top of your code and see if that
works.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top