Original Post
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.
// 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.}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;}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;}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;}// 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;}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.
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
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more