c语言中如何定义空字符串?

Python013

c语言中如何定义空字符串?,第1张

可以使用数值组负值,接着利用char数组进行负值。

include <string.h>

#include <stdio.h>

#include <stdlib.h>或者可以用string username[4]={“hoho“,“hohn“,“saturn“,“mike“}

//本意是username[0]=hoh

可以使用memset函数将字符串数组中所有元素全部设置为\0即可。

函数原型:void *memset(void *s, int ch, size_t n)

函数说明:将s中前n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。

示例:

#include <stdio.h>

#include <string.h>

 

int main()

{

char buf[256] = "hello world" //buf字符数组初始化

printf("%d\n", strlen(buf))  

memset(buf, 0x00, sizeof (char) * 256) //全部设置为0x00即\0字符

printf("%d\n", strlen(buf))   

return 0

}

#include<stdio.h>

int main(int argc, char* argv[])

{

 //二维数组包含10个字符串

 char str_list[10][100]

 //依次把字符串置为空串,也就是将首位置为'/0'

 for(int i = 0 i < 10 ++i)

 {

  str_list[i][0] = '\0'

 }

 return 0

}