C Programming Language, Pointer [closed]
I Stored "Hello" world in Character array and assigned the characteres into char pointer,
char a[100],*p;
p=a;
I found the length of the string using pointer then how should i find the position of the string. program开发者_C百科
char a[100],*p;
int lenth;
printf("Enter the string:");
gets(a);
p=a;
while(*p)
{
length++;
p++
}
printf("Length=%d",length);
i think if you want to get the length of the string you only need to use strlen(a) to get the length.
but if you want to mimic strlen you could write something like this
size_t length = 0;
for (char* p = a; p < a + sizeof(a) && *p; ++p, ++length );
do a search, p stores the result address, so p - a is the index.
char target = 'e';
for (p = a; *p && *p != target; ++p) {};
int result = p - a;
// if target doesn't exists result === Length
精彩评论