473,387 Members | 1,687 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,387 software developers and data experts.

compare char arrays

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
}
}
}

Oct 3 '07 #1
3 4178
DE*********@gmail.com 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
}
}
}
Oct 4 '07 #2
On Wed, 03 Oct 2007 15:45:45 -0700, DE*********@gmail.com 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.

Oct 4 '07 #3
On Wed, 03 Oct 2007 15:45:45 -0700, "DE*********@gmail.com"
<DE*********@gmail.comwrote:
>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
Oct 10 '07 #4

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

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
3
by: NathanV | last post by:
I'm trying to compare two byte's. I think I have to use binary operators. How can I tell if two bytes have the same value? Also, I have a function that is returning a byte value. Is there a...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
2
by: Florian G. Pflug | last post by:
Hi Since sometime yesterday, my postgresql (7.4.5) reports "ERROR: cannot compare arrays of different element types", when I analyze a specific table in my database. Here is the tables...
4
by: reva | last post by:
hi all!! can any one please help me in checking the two character arrays. in my code i need to compare a character array(seq) to that of hydrob and hydrop . if the seq has hydrob then it should be...
17
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;...
4
by: Google Groups | last post by:
If i take two structures with same members then,why cann't i compare those two structures?
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.