败血症早期图片:这段三级C语言的程序里的第6排的“memset(str,0,80);”是什么意思?

来源:百度文库 编辑:中科新闻网 时间:2024/03/29 17:14:55
void StrOL(void)
{ int i,j,k,s,m,strl;
char str[80];
for(i=0;i<maxline;i++)
{ strl=strlen(xx[i]);
memset(str,0,80);
s=k=0;
for(j=strl-1;j>=0;j--)
{ if(isalpha(xx[i][j])) k++;
else { for(m=1;m<=k;m++)
str[s++]=xx[i][j+m];
k=0;
}
if(!isalpha(xx[i][j]))
str[s++]=' ';
}
for(m=1;m<=k;m++)
str[s++]=xx[i][j+m];
str[s]='\0';
strcpy(xx[i],str); }
}
它的作用是什么呢?谢谢!

memset(str,0,80)是用0来初始化以地址str开始的80个连续单元..
memset函数具体的用法是:
memset() 函数常用于内存空间初始化。如: char str[100]; memset(str,0,100); memset用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘\0’;例:char a[100];memset(a, '\0', sizeof(a)); memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。 strcpy就只能拷贝字符串了,它遇到'\0'就结束拷贝;例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串长度(第一个‘\0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。

memset
Sets buffers to a specified character.

void *memset( void *dest, int c, size_t count );

Routine Required Header Compatibility
memset <memory.h> or <string.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

memset returns the value of dest.

Parameters

dest

Pointer to destination

c

Character to set

count

Number of characters

Remarks

The memset function sets the first count bytes of dest to the character c.

Example

/* MEMSET.C: This program uses memset to
* set the first four bytes of buffer to "*".
*/

#include <memory.h>
#include <stdio.h>

void main( void )
{
char buffer[] = "This is a test of the memset function";

printf( "Before: %s\n", buffer );
memset( buffer, '*', 4 );
printf( "After: %s\n", buffer );
}

Output

Before: This is a test of the memset function
After: **** is a test of the memset function