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 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
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
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
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 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." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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));
|
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()
|
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)...
|
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.
| |
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
|
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.
|
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)
{
|
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?
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |