I am using a simple string matching and searching.
Why does *arr
is never equal to point
though their values(why does I) are same? I am confused what is the cause of this?Is it related to strings or is there any other reason? Any help would be appreciated. I am sorry if the question is not clear.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int search(char ** arr, int size, char *point);
int main()
{
char *array[5]={0};
char original[50];
char toSearch[50];
char *point;
int size=5, searchIndex,i;
/* Copy a string into the original array */
strcpy(original, "why this is not equal");
/* Copy the first 10 characters of the original array into the newcopy array*/
point = memcpy(toSearch, original, 10);
/*string to search*/
array[2]="why this i";
searchIndex = search(array, size, point);
if(searchIndex == -1)
{
printf("%s does not exists in array \n", point);
} else
printf("%s is found at %d position.", toSearch, searchIndex + 1);
return 0;
}
int search(char ** arr, int size, char *point)
{
int index = 0;
// Pointer to last array element arr[size - 1]
char ** arrEnd = (arr + size - 1);
/* Why *arr!=point is never false,
even when both are giving out same values (why this I)?
*/
while(arr <= arrEnd && *arr!=point)
{
printf("%s == %s", *arr,point);
arr++;
index++;
}
if(arr <= arrEnd)
{
printf("%s after found \n",*arr);
return index;
}
return -1;
}
Output:
(null) == why this i
(null) == why this i
why this i == why this i
(null) == why this i
(null) == why this i
why this i does not exists in array
Thank You