473,800 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please help with strcmp()

Hello,
I'm having problems with string comparisons.
I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
3}. Now if I do strcmp(d1, d2[1]), it compares only the first
character, it seems. What is the correct solution?

Thanks,
andrej

--
echo ${girl_name} > /etc/dumpdates
Nov 13 '05 #1
3 2470
In 'comp.lang.c', Andrej Hocevar <dr******@volja .net> wrote:
I'm having problems with string comparisons.
I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
How is {1,2,3} a string?

Remainder : "a string is an array of characters terminated by a 0".

You want (maybe)

char d1[] = {1, 2, 3, 0};

or more likely

char d1[] = {'1', '2', '3', 0};

or

char d1[] = "123";
3}. Now if I do strcmp(d1, d2[1]), it compares only the first
character, it seems. What is the correct solution?


You must be sure that the adresses passed to strcmp() points to valid
strings.

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2
"Andrej Hocevar" <dr******@volja .net> wrote in message
news:sl******** *************@s onet.utopija.li nux...

I'm having problems with string comparisons.
I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
3}. Now if I do strcmp(d1, d2[1]), it compares only the first
character, it seems. What is the correct solution?


A string is a sequence of characters that ends with a null character. I
don't see any strings in your example, and I can't figure out what you mean
to do. Maybe you should be using strncmp or memcmp? In any case, the
second argument to strcmp--d2[1]--produces undefined results. There is no
d2[1]. There is only d2[0].

--
Russell Hanneken
rh*******@pobox .com
Nov 13 '05 #3
>I'm having problems with string comparisons.
I have data like char d1[3] = {1, 2, 3} and char d2[1][3] = {1, 2,
3}. Now if I do strcmp(d1, d2[1]), it compares only the first
character, it seems.
How did you determine this? You obviously used more code than
described above.
What is the correct solution?


A string has a '\0' terminator. The crap you are passing to
strcmp() above doesn't, so it's not a string.

Gordon L. Burditt
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1861
by: Kurt A. Kaylor | last post by:
Hey, I am trying to get some code I have written to work. Runs well until I make a request. The I get some problems with PHP related to an SQL statement. Here are the errors :Warning: mysql_result(): supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\veiworderform.php on line 25 (error goes to 76) Here is the code, any help that can be offered would be great. THANKS A TON !!
5
2293
by: John Douglass | last post by:
I'm fairly new to doing involved file i/o and I came across something weird with a program I'm writing (modifying MIDI data, if it makes any difference). With some input files, when I modify data and then output to a new file, the program will crash. The catch is that when I open up the output file to see what the program managed to write, it always makes it to an offset of xFFF. For example, with one file it crashed at 2FFF, another at...
66
5421
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
22
3282
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
3
1260
by: memol | last post by:
/*in this part i'm giving sum data but i have error...please talk with me about my program */ #include<iostream.h> #include<string.h> #include<conio.h> void point(int*,int*,int*); void goal(int*,int*); int name(char,int,int); int Time(int,int,int); int time(int,int,int); void main()
4
1784
by: Marcus | last post by:
you guys are funny. i thought this was comp.lang.c++... not comp.we.can.only.comment.on things.that.can.be.compiled.c++ back in the day in this group, the jibe replies would've looked like: "char*'s WILL strcmp, assuming you didn't mean those errors above, your error is somewhere else." times change i guess. thanks though joe, appreciate you taking the time to actually compile that. the error must be somewhere else.
6
2884
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
1
1400
by: koti688 | last post by:
Hi Mates, Can u please tell me whats happpenning in those. Please tell what ever u got. Outline the functinaliy in normal words. i am new to C++. I need to write SWIG wrapper which checks this C++ Code in perl. Please tell , what ever u understood . Thanks in advance #include <iostream> #include "stdafx.h"
8
1908
by: iPaul | last post by:
hi this is my code #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdlib.h> #include <string.h> #include <iostream>
0
9690
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
10501
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...
1
10250
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
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.