How to compare one character of a string to a char?

S

sieg1974

Hi,

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )
printf( "YES\n" );
}

Thanks,

Andre
 
E

Ed Morton

Hi,

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )

Get rid of the "*". directory[0] is a char (reasonable assumptions made....) but
*directory[0] is trying to dereference that char, hence the segmentation fault.

printf( "YES\n" );

May as well make that puts("YES");

Ed.
 
M

Martin Ambuhl

sieg1974 said:
Hi,

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )
printf( "YES\n" );
}

You are trying to use directory[0] is a pointer. Simply erase that '*':
if (directory[0] == '/') puts("YES");
or erase the indexing:
if (*directory == '/') puts("YES");

directory[n] is the same as *(directory + n). When n==0, this second
reduces to *directory.
 
B

Brett Frankenberger

Hi,

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )

Get rid of the "*". directory[0] is a char (reasonable assumptions made....) but
*directory[0] is trying to dereference that char, hence the segmentation fault.

directory[0] is a char. Dereferencing a char is a constraint violation
that must result in a diagnostic, and, with most compilers, will result
in no object file being produced.

As you note, directory[0]=='/' is probably what was intended ... but
it's also interesting that the code even compiled to the point of the
user being able to get a segmentation fault, and that suggests to me
that there is more to this picture than the code snipped the original
poster provided.

-- Brett
 
M

Mike Wahler

sieg1974 said:
Hi,

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )

if(directory[0] == '/')

or
if(*directory == '/')

or

if(*(directory + 0) == '/')


Yes, that last one seems a bit silly, I provided it
for 'completeness'.

-Mike
 
B

Ben Pfaff

how can I compare one character of a string to a char?
I have tried this to compare the first character of directory to '/',
but when I run this program I got a segmentation fault.

void makeBaseDir( char * directory )
{
if( *directory[ 0 ] == '/' )
printf( "YES\n" );
}

First, that won't compile because of the constraint violation
noted by others (and another problem). Second, this certainly
won't cause a segmentation fault, because there's no main()
function. You need to show us the code that calls this.
 

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
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top