I am writing a program where I need to check if an object matches a certain type. The way it's written so far, you need to compare the character-array of the object with the character-array of the category you are looking for.
I am using strcmp to do this, for the following code let's say that Object->Name returns a character-array equal to "TypeA" ...
char type[] = "TypeA";
if (0 == strcmp (Object->Name, type))
{
printf("Match!");
}
^This has worked well so far...
The only problem is that sometimes the character-array from Object->Name will have a 9 letter name in front of it. So Object->Name could be: "Chocolate_TypeA" or "FireTruck_TypeA"
I need to compare the last five characters with the value of type. I'm not sure how to do it.
My first idea was to use strncmp and specifying an index
char type[] = "TypeA";
if (0 == strncmp (type, Object->Name + 10))
{
printf("Match!");
}
This works if the program only receives 15 character character-arrays (such as "Firetruck_TypeA"), but it doesn't... if Object->Name gives "TypeA" then I get a segmentation fault.
What is an easy way to grab the last 5 characters of any character-array and compare them to type?