My professor wrote this program below. I was wondering why the find_c variable always changes when I run the program. Also, I'm confused about this line
Code:
i = find_c - string2;
"i" is the difference of the location of the character it's looking for (find_c) and string2, but what what is the value of string2? I was thinking it is the first index of the array because I remember my professor saying that the name of an array points to the first element.
http://i50.tinypic.com/1r53jo.png
Code:
#include <stdio.h>
#include <string.h>
#define MAXCHAR 256
void main(void)
{
int i;
char *find_c;
char string1[] = {"red"};
char string2[] = {"the color red is good"};
find_c = strchr(string2,'r');
i = find_c - string2;
printf("The first %c in \"%s\" occurs at position %d\n", find_c, string2, i);
find_c = strrchr(string2,'r');
i = find_c - string2;
printf("The last %c in \"%s\" occurs at position %d\n", find_c, string2, i);
} /* end main */