By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


On 9/8/2003 5:38 PM, sieg1974 wrote:
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.
}

Thanks,

Andre


Nov 13 '05 #2

P: n/a
sieg1974 wrote:
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.

--
Martin Ambuhl

Nov 13 '05 #3

P: n/a
In article <3F**************@Lucent.com>,
Ed Morton <mo****************@Lucent.com> wrote:


On 9/8/2003 5:38 PM, sieg1974 wrote:
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
Nov 13 '05 #4

P: n/a
sieg1974 <si******@yahoo.com> wrote in message
news:89**************************@posting.google.c om...
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

Nov 13 '05 #5

P: n/a
si******@yahoo.com (sieg1974) writes:
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.
--
Peter Seebach on C99:
"[F]or the most part, features were added, not removed. This sounds
great until you try to carry a full-sized printout of the standard
around for a day."
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.