开发者

C Programming Language, Pointer [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜