C库函数中strrchr函数的定义是什么?

函数的定义是什么?不是说声明,是说语句,是怎样的语句来实现这个函数的?不要自己写的,要库里定义的。... 函数的定义是什么?不是说声明,是说语句,是怎样的语句来实现这个函数的?不要自己写的,要库里定义的。 展开
 我来答
liuzitan990627
2014-02-05 · TA获得超过271个赞
知道小有建树答主
回答量:377
采纳率:0%
帮助的人:327万
展开全部

其实那个叫”实现“,不叫定义。定义是标准干的事。

各个C运行时库(C Library)有不同的实现(implementation)。

这里列举的是uClibc和glibc。

微软Visual C++ 的C库是不开源滴。


函数原型(定义):

extern char * strrchr (const char *s, int c);

uClibc 实现:(string/strrchr.c)

/*
 * Copyright (C) 2002     Manuel Novoa III
 * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

#include "_string.h"

#ifdef WANT_WIDE
# define Wstrrchr wcsrchr
#else
# define Wstrrchr strrchr
#endif

Wchar *Wstrrchr(register const  Wchar *s, Wint c)
{
register const Wchar *p;

p = NULL;
do {
if (*s == (Wchar) c) {
p = s;
}
} while (*s++);

return (Wchar *) p; /* silence the warning */
}
#ifndef WANT_WIDE
libc_hidden_weak(strrchr)
# ifdef __UCLIBC_SUSV3_LEGACY__
weak_alias(strrchr,rindex)
# endif
#endif

GNU libc 实现:(string/strrchr.c)

/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <
*/

#include <string.h>

#undef strrchr

/* Find the last occurrence of C in S.  */
char *
strrchr (const char *s, int c)
{
  const char *found, *p;

  c = (unsigned char) c;

  /* Since strchr is fast, we use it rather than the obvious loop.  */

  if (c == '\0')
    return strchr (s, '\0');

  found = NULL;
  while ((p = strchr (s, c)) != NULL)
    {
      found = p;
      s = p + 1;
    }

  return (char *) found;
}

#ifdef weak_alias
#undef rindex
weak_alias (strrchr, rindex)
#endif
libc_hidden_builtin_def (strrchr)
金色潜鸟
2014-02-05 · TA获得超过3.2万个赞
知道大有可为答主
回答量:1.3万
采纳率:89%
帮助的人:5957万
展开全部
C库函数:
strrchr 函数 用来返回 一个指针,指向 字符串中 最后一次 出现 该字符的 地点。
例如,在 字符串:
"This is a sample string";
中 找出 's' 最后出现的 位置,
返回 指针,指向 ...string 的 s
函数原型: char * strrchr ( const char *, int );
==================
完整程序如下:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a sample string";
char * pch;
pch=strrchr(str,'s');
printf ("Last occurence of 's' found at %d \n",pch-str+1);
return 0;
}

pch-str+1 算出 's' 最后出现的 位置 是第18个字符。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式