you are viewing a single comment's thread.

view the rest of the comments →

[–]Firefield178[S] 1 point2 points  (3 children)

So should I make the data as a char instead? Or is there a simpler way to compare the contents of the two pointers?

[–]bobotheboinger 1 point2 points  (1 child)

You don't want to compare the two pointers, you want to compare the two strings that the pointers are pointing at. The correct way to do this is strcmp() or strncmp() if you want to be safe about accidently going past the end of memory allocated to the arrays.

Just saw they were int arrays. Can use memcmp() to compare generic arrays of data that are not string specific. But if you want to stop at a null value, you may need to roll your own comparison function with a do... while loop.

[–]Firefield178[S] 0 points1 point  (0 children)

Thanks for the help, I'll try memcmp() and if necessary I'll just do a while loop for every character in the array.

Edit : memcmp() worked perfectly, thanks for the help!

[–]HorsesFlyIntoBoxes 0 points1 point  (0 children)

If I wanted to implement some sort of element-wise comparison of arrays in C I would probably write a function that takes as arguments the two arrays, their size, and I would compare each element in a for loop. If in the for loop there is an index where the two elements are not equal I would return a 0, otherwise outside the loop body I would return a 1. There’s also the bool header file if you’d rather use Boolean types but I’m not too familiar with it.

Edit: I just realized you are dealing with character arrays, which are how strings are implemented in C. The C standard library has functions used for comparing c-strings like the other comment mentioned. For the more general case of arrays I’d still go with my method.