Skip to main content
GameDev.net gamedev.net
🔒 Locked

Comparing Arrays

Started by MercenaryBK Jun 16, 2007 at 7:09 PM 8 replies 1.1k views
Original Post
MercenaryBK
MercenaryBK
I'm using a two, two dimensional arrays to store character data and I need to know how to compare the two to find out of they are the same or not. Any help would be appreciated.
erissian
erissian
To see if they're pointing at the same data, or to see if the data being pointed at is the same?
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
MercenaryBK
MercenaryBK
Oh, sorry. To see if the data pointed at is the same.
erissian
erissian
Basically you loop through the first dimension of each array, and compare the contents with strcmp. Note that strcmp returns 0 if the strings are equivalent.

// Two character arrays, the number of strings they hold, and the max length of those stringsint arraycmp(char** array1, char** array2, int m, int n) {  for (int i=0; i<m; i++) {    if (strncmp(array1,array2,n))      return 0;  }  return 1; // Hasn't failed, so it must be OK.}

We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
MercenaryBK
MercenaryBK
Even if I'm using two, two dimensional arrays I only loop through one dimension with strcmp?
erissian
erissian
Well, the two dimensional character array can be written in one way as char[m][n]. When looked at another way, it's a one dimensional array whose elements are of type char[n].

Since strncmp compares two entire strings (char[n]) at once, you're left only with the [m] dimension to iterate through.

Unless I've mistaken your meaning, you're expecting the strings to be in the same order. So this is essentially a process of iterating through the [m] dimension, comparing element i of each array to each other.

Say you wanted to compare two integer arrays of size 3. You might just do it like this:

int compare_integer_arrays ( int some_integers[3], int other_integers[3] ) {  if ( some_integers[0] != other_integers[0] ) return 0;  if ( some_integers[1] != other_integers[1] ) return 0;  if ( some_integers[2] != other_integers[2] ) return 0;  return 1;}


Now let's say that you had a two dimensional array of integers, say they were both int[5][3]. It's the same process, run through that first dimension, and compare the second dimensions against each other. Reusing the function above, we might write:

int compare_2D_integer_arrays ( int more_integers[5][3], int even_more_integers[5][3] ) {  if ( !compare_integer_arrays( more_integers[0], even_more_integers[0] ) return 0;  if ( !compare_integer_arrays( more_integers[1], even_more_integers[1] ) return 0;  if ( !compare_integer_arrays( more_integers[2], even_more_integers[2] ) return 0;  if ( !compare_integer_arrays( more_integers[3], even_more_integers[3] ) return 0;  if ( !compare_integer_arrays( more_integers[4], even_more_integers[4] ) return 0;  return 1;}


So this is coming closer to want you want. If we wanted to make it a little more general, we would have something to indicate the size of the array we're using. So we'll rewrite these so that you specify the size of the arrays.

int compare_integer_arrays ( int *some_integers, int *other_integers, int n ) {  for {int i=0; i<n; i++ )    if ( some_integers != other_integers ) return 0;  return 1;}int compare_2D_integer_arrays ( int **more_integers, int **even_more_integers, int m, int n ) {  for (int i=0; i<m; i++ )    if ( !compare_integer_arrays( more_integers, even_more_integers, n ) return 0;  return 1;}


Now this is in the same form that we used above. There are two major differences:
1. We used character arrays before, not integers.
2. We had to write our own comparison for 1 dimensional integer arrays. For characters, a function like that already exists: strncmp.

Let me know if you have more questions.

Side note: Technically, we didn't have to write our own int[n] comparison function; we could have just used memcmp. I was being illustrative.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Verg
Verg
Here's an more simple example using "memcmp"

If you know the arrays are the same size, you can use "memcmp" in one fell swoop.

// overly simplified, for example#include <memory.h>int main(){	char array1[5][5],		 array2[5][5];	::memset(array1[0], 0, (sizeof(char) * 5 * 5));	::memset(array2[0], 0, (sizeof(char) * 5 * 5));	if(::memcmp(array1[0], array2[0], (sizeof(char) * 5 * 5)) == 0)	{		int i = 0; // this is just a dummy line to set your breakpoint on		//success	}	else	{		int i = 0;		// fail	}	return 0;}
Iftah
Iftah
Quote:
Original post by MercenaryBK
I'm using a two, two dimensional arrays to store character data and I need to know how to compare the two to find out of they are the same or not. Any help would be appreciated.


character data = null terminated strings?

if so erissian strncmp would work,
if it is just array of characters you'd want to use the Verg's memcmp version

strcmp will stop comparing when it reaches the '\0' character
erissian
erissian
Quote:
Original post by Iftah
Quote:
Original post by MercenaryBK
I'm using a two, two dimensional arrays to store character data and I need to know how to compare the two to find out of they are the same or not. Any help would be appreciated.


character data = null terminated strings?

if so erissian strncmp would work,
if it is just array of characters you'd want to use the Verg's memcmp version

strcmp will stop comparing when it reaches the '\0' character


Ah true, I made the assumption that we were talking about strings.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
erissian
erissian
Ah; I got a PM with more detail. It's not strings, and he needs more information on the character level, so evaluating the whole chunk is no good either.

C'est la vie,
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.