473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2450
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**********@noos.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*********************@sonet.utopija.linux.. .

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
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:...
5
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...
66
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...
22
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...
3
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...
4
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:...
6
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
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++...
8
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.