memchr函数, 如何自己写一个函数代替这个函数的功能
memchr函数, 怎么自己写一个函数代替这个函数的功能
各位帮帮忙吧。。。。
------解决思路----------------------
------解决思路----------------------
------解决思路----------------------
楼上的别开玩笑了, 这里能用strlen吗?buf 参数没有结束符这么办
------解决思路----------------------
先
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
右边Visual C++ 2010 Express下面的Select language...下拉选‘简体中文’,再按Install Now按钮
再参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\memchr.c
各位帮帮忙吧。。。。
------解决思路----------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void * mymemchr(void *buf, char ch, unsigned count)
{
unsigned i;
char *p = (char *)buf;
for (i = 0; i < count; i++)
{
if (p[i] == ch)
{
return (p + i);
}
}
return NULL;
}
int main()
{
printf("%c\n", *(char*)mymemchr("abcd123abe", 'b', 9));
return 0;
}
------解决思路----------------------
//首先通过MSDNL 查询到 此函数原型
/*
void *memchr( const void *buf, int c, size_t count ); 函数原型,void * 是为了兼容,如:int * char * float * 都可以自动转成void *
Parameters 形参说明
buf
Pointer to buffer 指向缓冲区的指针
c
Character to look for 要查找的字符
count
Number of characters to check 指定数量,此数量是 buf 的前count个字节
Return Values 返回值
If successful, memchr returns a pointer to the first location of c in buf. Otherwise it returns NULL.
如果执行成功,这个函数返回指向 缓冲区buf中第一个c的 指针 ,否则返回NULL
*/
/*Remarks 功能简介
The memchr function looks for the first occurrence of c in the first count bytes of buf. It stops when it finds c or when it has checked the first count bytes.
*/ /*这个函数在缓冲区 buf 的前 count 个字节里查找 给定字符 c ,当它找到 c 或者 在buf的前 count个字节里没有找到则停止 */
//下面我们来实现它
#include <stdio.h>
#include <string.h>
void *mymemchr( const void *buf, int c, size_t count )
{
//定义临时变量
char * tem=(char *)buf;//这操作是 将buf的类型强转为char *
if (count > strlen(tem)) //检测越界情况 如: buf="abcdef" count=9 c='g' 这样会越界
{
count=strlen(tem);
}
for (unsigned i=0;i<count;i++)
{
if (tem[i]==c)
{
return tem+i;
}
}
return NULL;
}
void main()
{
char *str="abcdefhik";
printf("%c\n",*(char*)mymemchr(str,'d',6));
}
------解决思路----------------------
楼上的别开玩笑了, 这里能用strlen吗?buf 参数没有结束符这么办
void * my_memchr(const void *s, int c, size_t n)
{
if (n != 0) {
const unsigned char *p = s;
do {
if (*p++ == (unsigned char)c)
return ((void *)(p - 1));
} while (--n != 0);
}
return (NULL);
}
------解决思路----------------------
先
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
右边Visual C++ 2010 Express下面的Select language...下拉选‘简体中文’,再按Install Now按钮
再参考
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\memchr.c
/***
*memchr.c - search block of memory for a given character
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines memchr() - search memory until a character is
* found or a limit is reached.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *memchr(buf, chr, cnt) - search memory for given character.
*
*Purpose:
* Searches at buf for the given character, stopping when chr is
* first found or cnt bytes have been searched through.
*
*Entry:
* void *buf - memory buffer to be searched
* int chr - character to search for
* size_t cnt - max number of bytes to search
*
*Exit:
* returns pointer to first occurence of chr in buf
* returns NULL if chr not found in the first cnt bytes
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl memchr (
const void * buf,
int chr,
size_t cnt
)
{
while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
buf = (unsigned char *)buf + 1;
cnt--;
}
return(cnt ? (void *)buf : NULL);
}