473,657 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 39274


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
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
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
sieg1974 <si******@yahoo .com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
2258
by: JKop | last post by:
Haven't been able to find such a thing. Can anyone please inform me of a Standard C++ function for comparing two strings without regard to case. Both for working with "char*", and with "std::string". Thanks, -JKop
8
3726
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...) return (::memcmp(_First1, _First2, _Count));
8
62646
by: Sharon | last post by:
hi, I want to compare character, if the string contains character "-" then it will print Hello on the screen however, neither method (1) nor method (2) work in the code below: so what the correct code should be? thanks! #include <stdio.h> #include <stdlib.h> int main()
8
4484
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working with weird stuff, just strings, int, bools and DataTime values) My fields values for this record, for convenience are also in an array of objects. Now I am trying to write code like the following. private void processData (object d, object a)...
10
14476
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives the "right" (i.e.wysiwyg) answer while compare() does not.
9
11358
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an example, let's say the map key is char* strings, and I want the comparison based on the nth character in the string. When the map is instantiated, I need to somehow specify n. So I might have something like this: class MyClass
3
3681
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare strFileName with strFileName1.if there is any mistek or some word is missing in second string then that words should be highlight(in sense change the color of that words) by the button click event. pls somebody give an idea to me.
0
2303
by: Diego Martins | last post by:
Hi! The following code snippet: namespace { bool charCompare(char a, char b) { return tolower(a) < tolower(b); } } bool compareString(const std::string & s1, const std::string & s2) {
17
3463
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0; while( data != *((short*) "DA") ) j++; But this doesn't work. The char obviously has an equivalent 16-bit value so how do I get that info in a simple way?
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.