VS2013 在debug模式下编译没有错误,但使用release模式,就有一堆错误

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include "字符串.h"

int main()
{
Mystring string1;
initwithstring(&string1, "xhqld");
printstring(&string1);

backaddchar(&string1, 'd');
printstring(&string1);

backaddstr(&string1, "forever");
printstring(&string1);

char * p = NULL;
p = findfirstchar(&string1, 'd');
*p = 'a';
printstring(&string1);

char * p1 = mystrstr(string1.p, "ever");
if (p1 != NULL)
    printf("%c\n", *p1);
else
    printf("not find\n");
printf("--------------\n");
deletechar(&string1, 'a');
printstring(&string1);

printf("--------------\n");
deletestring(&string1, "for");
printstring(&string1);

printf("--------------\n");
addchar(&string1, 'W', 'e');
printstring(&string1);

printf("--------------\n");
addstring(&string1, "lol",'W');
printstring(&string1);
system("pause");

return 0;

}

 #include<stdio.h>
#include<stdlib.h>
struct CString
{
    char * p;//保存字符串的首地址
    int length;//字符串长度
};

typedef struct CString  Mystring;

void init(Mystring * string);//初始化字符串
void initwithlength(Mystring * string, int length);
void initwithstring(Mystring *string, char * copystring);//
void printstring(Mystring * string);//打印字符串
int mystrlen(char * p);//求字符串长度
char * mystrcpy(char * dest, const char * source);//字符串复制
void backaddchar(Mystring * string, char ch);//尾部增加一个字符
void backaddstr(Mystring * string, char * str);//尾部增加一个字符串
char * findfirstchar(Mystring * string, char ch);//查找第一个匹配字符
char * mystrchr(char * str, char ch);//查找字符
char * mystrstr(char * dest, char * str);//字符串比较函数
int deletechar(Mystring * string, char ch);//删除一个字符
int deletestring(Mystring * string, char * str);//删除一个字符串
void addchar(Mystring * string, char ch, char searchchar);//任意增加字符
void addstring(Mystring * string, char *str, char searchchar);//任意增加字符串

#define _CRT_SECURE_NO_WARNINGS
#include"字符串.h"
#include
void init(Mystring * string)//初始化字符串
{
string->length = 0;
string->p = NULL;
}

void initwithlength(Mystring * string, int length)
{
//Mystring * p = (char *)malloc(sizeof(char)*length);
Mystring * p = (char *)calloc(length, sizeof(char));//calloc为分配的内存清零,malloc不清零
}

void initwithstring(Mystring *string, char * copystring)//
{
int length = mystrlen(copystring);
string->p = (char *)calloc(length + 1, sizeof(char));
mystrcpy(string->p, copystring);
string->length = length + 1;
}

void printstring(Mystring * string)//打印字符串
{
printf("%s\n", string->p);
}

int mystrlen(char * p)
{
if (p != NULL)
{
int length = 0;
while (*p++ != '\0')
length++;
return length;
}
else
return -1;//打开失败

}

char * mystrcat(char * dest, const char * source)
{

if (dest == NULL || source == NULL)
    return NULL;
char * pdest = dest;
while (*pdest != '\0')
    pdest++;
while (*source != '\0')
    *pdest++ = *source++;
*pdest = '\0';
return dest;

}

char * mystrcpy(char * dest, const char *source)
{
if (dest == NULL || source == NULL)
return NULL;
char * pdest = dest;
while (*source != '\0')
*pdest++ = *source++;
*pdest = '\0';
return dest;
}

void backaddchar(Mystring * string, char ch)//尾部增加一个字符
{

    if (mystrlen(string->p) + 2 > string->length)//表达式成立,则表示已满
    {

        string->p = (char *)realloc(string->p, string->length + 2);
        string->p[string->length - 1] = ch;
        string->p[string->length] = '\0';
        printf("%s\n", string->p);
        string->length += 2;

    }
    else
    {
        string->p[mystrlen(string->p)] = ch;
        string->p[mystrlen(string->p) + 1] = '\0';
    }   

}
void backaddstr(Mystring * string, char * str)//尾部增加一个字符串
{
if (str != NULL)
{
if ( mystrlen(string->p) + mystrlen(str) +1 > string->length)
{
int length = mystrlen(str);
string->p = (char *)realloc(string->p, string->length + length + 1);
mystrcat(string->p, str);
string->p[string->length + length] = '\0';
string->length = string->length + length + 1;
}
else
{
mystrcat(string->p, str);

        string->p[mystrlen(string->p) + mystrlen(str)] = '\0';
    }

}

}

char * findfirstchar(Mystring * string,const char ch)
{
char * p = mystrchr(string->p, ch);
return p;
}

char * mystrchr(char * str, char ch)
{
if (str != NULL || ch != '\0')
{
char * pstr = str;
while (*pstr != '\0')
{
if (*pstr == ch)
return pstr;
pstr++;
}
return NULL;
}
}

char * mystrstr(char * dest, char * str)
{
char * pdest = dest;
char * pstr = str;
int str_length = mystrlen(str);
int dest_length = mystrlen(dest);
if (str_length <= dest_length)
{
int flag = 0;
int i = 0;
while (dest_length - i >= str_length && flag == 0)
{
if (pdest[i] == pstr[0])
{
flag = 1;
for (int k = 1; k < str_length && flag == 1; k++)
flag = pdest[i + k] == pstr[k] ? 1 : 0;
}
i++;
}
return flag == 1 ? &pdest[i - 1] : NULL;
/*if (flag == 1)
return &pdest[i - 1];
else
return NULL;*/
}
}

int deletechar(Mystring * string, char ch)//删除一个字符
{
char * p = mystrchr(string->p, ch);
if (p != NULL )
{
while (*p != '\0')
*p++ = *(p + 1);
*--p = '\0';
return 1;
}
return 0;

}

int deletestring(Mystring * string, char * str)//删除一个字符串
{
char * p = mystrstr(string->p, str);//查找字符串位置
if (p != NULL)
{
int str_length = mystrlen(str);//计算被查找子串的长度
int string_length = mystrlen(string->p);//计算母串string的长度
char * pend = p + str_length;//pend指向string->p中被查找子串的尾部
while (* pend != '\0')//把被查找子串后面的字符前移,覆盖被查找的子串
*p++ = *pend++;
string->p[string_length - str_length] = '\0';//为删除str子串后的string母串设置新的字符串结束符
return 1;
}
return 0;
}

void addchar(Mystring * string, char addchar, char searchchar)//任意增加字符
{
char * position = mystrchr(string->p, searchchar);
if (position != NULL)
{
int string_length = mystrlen(string->p);//求母串的长度
int position_length = mystrlen(position);//求从插入字符的前面位置开始到字符串末尾处,要移动字符的个数
if (string_length + 2 > string->length)//判断是否满了
{
string->p = (char *)realloc(string->p, string_length + 2);
for (int i = string_length; i > string_length - position_length; i--)//从插入位置开始将剩下的字符窜后移
string->p[i] = string->p[i - 1];
string->p[string_length + 1] = '\0';//重新设置字符串结束符
*position = addchar;//插入字符串
string->length += 1;//字符串实际长度增加
}
else
{
for (int i = string_length; i > string_length - position_length; i--)//从插入位置开始将剩下的字符窜后移
string->p[i] = string->p[i - 1];
string->p[string_length + 1] = '\0';//重新设置字符串结束符
*position = addchar;//插入字符串
}
}

}
void addstring(Mystring * string, char *str, char searchchar)//任意增加字符串

{
printf("test:%d\n", string->length);
char * position = mystrchr(string->p, searchchar);
if (position != NULL)
{
int string_length = mystrlen(string->p);//求母串的长度
int position_length = mystrlen(position);//求从插入字符的前面位置开始到字符串末尾处,要移动字符的个数
int str_length = mystrlen(str);//求要添加子串的长度
if (string_length + str_length + 1 > string->length)//判断以前分配的空间是否够用
{
string->p = (char *)realloc(string->p, string_length + str_length + 1);
string->length += str_length;
for (int i = string_length; i >= string_length - position_length; i--)//移动字符,连同字符串结束符一起移动
string->p[i + str_length] = string->p[i];
for (int i = string_length - position_length; i <= string_length - 1; i++)
string->p[i] = *str++;

    }
    else
    {
        for (int i = string_length; i >= string_length - position_length; i--)//移动字符,连同字符串结束符一起移动
            string->p[i + str_length ] = string->p[i];
        for (int i = string_length - position_length; i < string_length - position_length + str_length; i++)
        {
            printf("test:%d,%c\n", i, string->p[i]);
            string->p[i] = *str++;
            printf("test:%d,%c\n", i, string->p[i]);
        }

    }
}

}

图片说明

错误代码片段如下:
char * position = mystrchr(string->p, searchchar);
if (position != NULL)

            char * p = mystrchr(string->p, ch);
      return p;

            char * p = findfirstchar(&string1, 'd');
     *p = 'a';

1个回答

 双击每个错误,修改下。
提示你没有初始化变量,比如
int i;
你就修改为
int i = 0;
总之给一个初始值。

不是所有路径都有返回值,你就加上返回值。
比如
int foo(int i)
{
    if (i == 0)
            return 1;
}
这里,把return写在了if中(或者循环,switch等),所以不是所有路径都有返回值。
修改为
int foo(int i)
{
    if (i == 0)
            return 1;
    return xxx;
}
qq_25422497
qq_25422497 回复caozhy: 了解,谢谢
接近 5 年之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复qq_25422497: release和 debug的编译器警告等级不同。编译器可以容忍一些不规范的写法。
接近 5 年之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复qq_25422497: 本身你就应该养成好习惯。
接近 5 年之前 回复
qq_25422497
qq_25422497 您好,按照你说的做了,果然编译通过了,这难道是release模式下,编译器强制要求这么做么?是为了避免不小心使用未始化的指针或变量么??
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问