I input a string and then try to find an address of a char
within the string but the problem is that I am unable to find the address of the same char
in the string using pointers.
For example when input is "ALLEN"
I need the addresses of both 'L'
s but my program only prints the address of the first 'L'
.
I tried if ... else
and a for
-loop but I can't solve the problem.
#include <stdio.h>
#include <string.h>
main()
{
char a, str[81], *ptr;
printf("\nEnter a sentence:");
gets(str);
printf("\nEnter character to search for:");
a = getchar();
ptr = strchr(str,a);
/* return pointer to char*/
printf( "\nString starts at address: %d",str);
printf("\nFirst occurrence of the character (%c) is at address: %d ", a,ptr);
}