char * strchr (const char *str, int c);

本文介绍了strchr函数的用法及示例,strchr用于在给定字符串中查找指定字符首次出现的位置,并返回该字符的地址。文章通过示例演示了如何使用strchr函数定位特定字符。
头文件:#include <string.h>

strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:
    char * strchr (const char *str, int c);

【参数】str 为要查找的字符串,c 为要查找的字符。

strchr() 将会找出 str 字符串中第一次出现的字符 c 的地址,然后将该地址返回。

注意:字符串 str 的结束标志 NUL 也会被纳入检索范围,所以 str 的组后一个字符也可以被定位。

【返回值】如果找到指定的字符则返回该字符所在地址,否则返回 NULL。

返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串位置。设字符在字符串中首次出现的位置为 i,那么返回的地址可以理解为 str + i。

提示:如果希望查找某字符在字符串中最后一次出现的位置,可以使用  strrchr()  函数。

【实例】查找字符5首次出现的位置。
   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main(){
  5.     char *s = "0123456789012345678901234567890";
  6.     char *p;
  7.     p = strchr(s, '5');
  8. printf("%ld\n", s);
  9.     printf("%ld\n", p);
  10. system("pause");
  11.     return 0;
  12. }
输出结果:
12016464
12016469
/*-------------------------------------------------------------------------- STRING.H String functions for C251 Version 4. Copyright (c) 1995-2007 Keil Elektronik GmbH and Keil Software, Inc. All rights reserved. --------------------------------------------------------------------------*/ #pragma SAVE #pragma PARM251 #ifndef _SIZE_T #define _SIZE_T typedef unsigned int size_t; #endif #ifndef NULL #define NULL ((void *) 0L) #endif #if (__C251__ >= 200) extern char *strcat (char *s1, const char *s2) reentrant; extern char *strncat (char *s1, const char *s2, size_t n) reentrant; extern char strcmp (const char *s1, const char *s2) reentrant; extern char strncmp (const char *s1, const char *s2, size_t n) reentrant; extern char *strcpy (char *s1, const char *s2) reentrant; extern char *strncpy (char *s1, const char *s2, size_t n) reentrant; extern size_t strlen (const char *) reentrant; extern char *strchr (const char *s, char c) reentrant; extern int strpos (const char *s, char c) reentrant; extern char *strrchr (const char *s, char c) reentrant; extern int strrpos (const char *s, char c) reentrant; extern size_t strspn (const char *s, const char *set) reentrant; extern size_t strcspn (const char *s, const char *set) reentrant; extern char *strpbrk (const char *s, const char *set) reentrant; extern char *strrpbrk (const char *s, const char *set) reentrant; extern char memcmp (const void *s1, const void *s2, size_t n) reentrant; extern void *memcpy (void *s1, const void *s2, size_t n) reentrant; extern void *memchr (const void *s, char val, size_t n) reentrant; extern void *memccpy (void *s1, const void *s2, char val, size_t n) reentrant; extern void *memmove (void *s1, const void *s2, size_t n) reentrant; extern void *memset (void *s, char val, size_t n) reentrant; extern void far *fmemset (void far *s, char val, unsigned int n) reentrant; extern void xdata *xmemset (void xdata *s, char val, unsigned int n) reentrant; extern void far *fmemcpy (void far *s1, void far *s2, unsigned int n) reentrant; extern void xdata *xmemcpy (void xdata *s1, void xdata *s2, unsigned int n) reentrant; extern unsigned long hstrlen (const char huge *s) reentrant; extern char hstrcmp (const char huge *s1, const char huge *s2) reentrant; extern char huge *hstrcpy (char huge *s1, const char huge *s2) reentrant; #pragma PARM4 // allow a maximum of 4 long as register parameters extern char huge *hstrncpy (char huge *s1, const char huge *s2, unsigned long n) reentrant; extern char hmemcmp (const void huge *s1, const void huge *s2, unsigned long len) reentrant; extern void huge *hmemcpy (void huge *s1, const void huge *s2, unsigned long len) reentrant; extern void huge *hmemchr (const void huge *ptr, char val, unsigned long len) reentrant; extern char huge *hmemccpy (char huge *dest, const char huge *src, char val, unsigned long len) reentrant; extern void huge *hmemmove (void huge *s1, const void huge *s2, unsigned long len) reentrant; extern void huge *hmemset (void huge *ptr, char val, unsigned long len) reentrant; #else extern char *strcat (char *s1, char *s2); extern char *strncat (char *s1, char *s2, int n); extern char strcmp (char *s1, char *s2); extern char strncmp (char *s1, char *s2, int n); extern char *strcpy (char *s1, char *s2); extern char *strncpy (char *s1, char *s2, int n); extern int strlen (char *); extern char *strchr (const char *s, char c); extern int strpos (const char *s, char c); extern char *strrchr (const char *s, char c); extern int strrpos (const char *s, char c); extern int strspn (char *s, char *set); extern int strcspn (char *s, char *set); extern char *strpbrk (char *s, char *set); extern char *strrpbrk (char *s, char *set); extern char memcmp (void *s1, void *s2, int n); extern void *memcpy (void *s1, void *s2, int n); extern void *memchr (void *s, char val, int n); extern void *memccpy (void *s1, void *s2, char val, int n); extern void *memmove (void *s1, void *s2, int n); extern void *memset (void *s, char val, int n); #endif #pragma RESTORE C:\Keil_v5\C251\Inc\string.h(22): error C25: syntax error near '"string"' 编译错误
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值