开发者

C Programming Language, Pointer [closed]

开发者 https://www.devze.com 2023-04-11 19:07 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
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

精彩评论

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

关注公众号