Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > compare char arrays

Reply
Thread Tools

compare char arrays

 
 
DERosenblum@gmail.com
Guest
Posts: n/a
 
      10-03-2007
Hi, I'm a total newbie at programming. I am looking to compare the an
element of array2 with the next element of array2, and if equal, put a
1 in the same spot of array3.

For example, if array2[2][3] has the same value as array2[3][3], then
I want the value of array3[2][3] to be 1. My code looks like this,
thanks!!


char array2[5][10][20];
char array3[5][10];

//arrays are already populated

for(int a=0; a<5; a++)
{
for(i=0; i<10; i++)
{
if(array2[a][i] == array2[a+1][i]
{
array3[a][i]=1
}
}
}

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      10-04-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi, I'm a total newbie at programming. I am looking to compare the an
> element of array2 with the next element of array2, and if equal, put a
> 1 in the same spot of array3.
>
> For example, if array2[2][3] has the same value as array2[3][3],


array2[N+1][P] is _not_ the next element to array2[N][P].

> then I want the value of array3[2][3] to be 1. My code looks like this,
> thanks!!
>
>
> char array2[5][10][20];
> char array3[5][10];
>
> //arrays are already populated
>
> for(int a=0; a<5; a++)


C++ style comments and mixed code and declarations have been standardised
with the latest C standard, i.e., C99, which is not widely implemented. So
your code above may not compile under certain configurations.

> {
> for(i=0; i<10; i++)


Also symbolic constants might be better than hardcoded literals. They make
change easy and centralised.

> {
> if(array2[a][i] == array2[a+1][i]


Now here is a problem. When 'a' is four 'a+1' refers outside the bounds of
array2 and invokes undefined behaviour. To prevent this you need to keep
track of your loop control variables more closely.

> {
> array3[a][i]=1
> }
> }
> }


 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      10-04-2007
On Wed, 03 Oct 2007 15:45:45 -0700, (E-Mail Removed) wrote:

> Hi, I'm a total newbie at programming. I am looking to compare the an
> element of array2 with the next element of array2, and if equal, put a
> 1 in the same spot of array3.
>
> For example, if array2[2][3] has the same value as array2[3][3], then
> I want the value of array3[2][3] to be 1. My code looks like this,
> thanks!!
>
>
> char array2[5][10][20];
> char array3[5][10];
>
> //arrays are already populated
>
> for(int a=0; a<5; a++)
> {
> for(i=0; i<10; i++)
> {
> if(array2[a][i] == array2[a+1][i]

Arrays evaluate to pointers to their first argument. Since
&array2[a][i][0] cannot be &array2[a+1][i][0] this is always
false. Try !memcmp(array2[a][i], array2[a+1][i], 20).
> {
> array3[a][i]=1
> }
> }
> }


--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-10-2007
On Wed, 03 Oct 2007 15:45:45 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>Hi, I'm a total newbie at programming. I am looking to compare the an
>element of array2 with the next element of array2, and if equal, put a
>1 in the same spot of array3.
>
>For example, if array2[2][3] has the same value as array2[3][3], then
>I want the value of array3[2][3] to be 1. My code looks like this,
>thanks!!
>
>
>char array2[5][10][20];
>char array3[5][10];
>
>//arrays are already populated
>
>for(int a=0; a<5; a++)
>{
> for(i=0; i<10; i++)
> {
> if(array2[a][i] == array2[a+1][i]


array2[a][i] is itself an array. In this context, the expression
evaluates to &array[a][i][0]. Similarly, the next expression
evaluates to &array2[a+1][i][0]. It should be obvious that these two
expressions can never be equal. If you are trying to determine if all
20 elements of array[a][i] are equal to the corresponding 20 elements
of array2[a+1][i], look into using memcmp.

Also note that when a is 4, you invoke undefined behavior since
array2[5][i] does not exist.

> {
> array3[a][i]=1
> }
> }
>}



Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Compare every bit of char versus unsigned char jamx C Programming 11 03-12-2006 08:06 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM



Advertisments