🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #303

Re: char array compare

#303
From: Bart van Ingen Schenau
Date: Fri, 25 Jan 2002 17:55
46 lines
2083 bytes
On Sat, 26 Jan 2002 01:10:24 +0800, "EL" <lameddie@yahoo.com> wrote:

>Help!
>
>How can i compare a char array from defined structure with input char?
>I can't get the expected result from the following coding. Pls help!
>
>
>struct mystr
>{
>char id[7], name[21];
>};
>
>mystr s[50];
>
>void main()

According to the definitions of the C and C++ languages should main
return an int, so
  int main()
>{
>  int i, j ;
>  char vid[7];
>
<snip>
>   if (vid == s[i].id)

There you are comparing the *addresses* of vid and s[i].id. To compare
the contents of the char arrays, use strcmp():
  if (strcmp(vid, s[i].id) == 0) /* they are equal */

>   {
>   cout << "\nFound id: " << s[i].id;
>   }
>   else
>   {
>   cout << "\nNot found you id " << vid;
>   }
>  }
>}
>
Bart v Ingen Schenau
--
Remove NOSPAM to mail me directly
FAQ for clc: http://www.eskimo.com/~scs/C-faq/top.html
FAQ for acllc-c++: http://snurse-l.org/acllc-c++/faq
Technical Details
Message-ID: <fp635uomuprbjm6m34tgkkhqs72631mpi4@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!surfnet.nl!news.nic.utwente.nl!not-for-mail
References: <a2s339$ngo3@imsp212.netvigator.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,70272e41ccb8f8b6
X-Google-Attributes: gidf8d38,public
X-Google-Thread: 109fba,70272e41ccb8f8b6
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,70272e41ccb8f8b6
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2002-01-25 09:51:55 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!surfnet.nl!news.nic.utwente.nl!not-for-mail
From: Bart van Ingen Schenau <bvisNOSPAM@universalmail.com>
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++
Subject: Re: char array compare
Date: Fri, 25 Jan 2002 18:55:06 +0100
Organization: University of Twente, Enschede, The Netherlands
Lines: 46
Message-ID: <fp635uomuprbjm6m34tgkkhqs72631mpi4@4ax.com>
References: <a2s339$ngo3@imsp212.netvigator.com>
NNTP-Posting-Host: adsl219110.adsl.utwente.nl
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Server-Date: 25 Jan 2002 17:51:52 GMT
X-Newsreader: Forte Agent 1.8/32.548