现在的位置: 首页 > 综合 > 正文

字符串常量与字符串变量

2018年04月09日 ⁄ 综合 ⁄ 共 538字 ⁄ 字号 评论关闭

定义:在一个双引号“ ”内的字符序列或者转义字符序列称为字符串常量

例如:“HI HI!”  “a”  “\n\t”

这些字符串常量是不能改变的,如果试图改变指针所指向的内容是错误的

因为字符串常量是存在静态内存区的,不可以改变

字符串变量,在c语言中没有纯粹的c语言字符串变量,可以通过一个字符数组来体现,这样就

可以对字符数组中的内容进行改变!

附一段代码,该代码将字符串进行分割:

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

static char* re;

static char* sp_strsep(char **s, const char *del)
{
    char *d, *tok;
    if (!s || !*s) return NULL;
    tok = *s;
    d = strstr(tok, del);
    if (d) 
		{
            *s = d + strlen(del);
            *d = '\0';
         }
	else 
         {
            *s = NULL;
         }
  return tok;
}

int main() 
{
    char *ss="chenwenshi";
    const char *dd="wen";
    re=sp_strsep(&ss,dd);
    printf("ss=%s,dd=%s\n",ss,dd);
    printf("%s\n",re);
    return 0;
}

抱歉!评论已关闭.