程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> visual studio-VS2013 在debug模式下編譯沒有錯誤,但使用release模式,就有一堆錯誤

visual studio-VS2013 在debug模式下編譯沒有錯誤,但使用release模式,就有一堆錯誤

編輯:編程綜合問答
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';

最佳回答:


 雙擊每個錯誤,修改下。
提示你沒有初始化變量,比如
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;
}
caozhy
caozhy
qq_25422497
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved