源代碼 :#include "stdafx.h"
#include "iostream"
using namespace std;
#define MAXLEN 200
char *strsub(char *s, int i, int len)
{
int j, slen;
static char t[MAXLEN];
slen = strlen(s);
if (i>slen || i<1 || len<0 || len>slen - i + 1)
cout << "錯誤,請重新輸入!!!";
return 0;
for (j = 0; j < len; j++)
t[j] = s[i + j - 1];
t[j] = '\0';
return (t);
}
void main()
{
int len, i;
char s[MAXLEN];
char *p;
cout << "請輸入原字串:";
cin >> s;
cout << "請輸入子串的起始位置:";
cin >> i;
cout << "請輸入子串的長度:";
cin >> len;
p = strsub(s, i, len);
cout << p << endl;
}
錯誤:0x01236571 處的第一機會例外(在 ConsoleApplication2.exe 中): 0xC0000005: 讀取位置 0x00000000 時發生訪問沖突。
0x01236571 處有未經處理的例外(在 ConsoleApplication2.exe 中): 0xC0000005: 讀取位置 0x00000000 時發生訪問沖突。
0x01236571 處的第一機會例外(在 ConsoleApplication2.exe 中): 0xC0000005: 讀取位置 0x00000000 時發生訪問沖突。
0x01236571 處有未經處理的例外(在 ConsoleApplication2.exe 中): 0xC0000005: 讀取位置 0x00000000 時發生訪問沖突。
程式“[7804] ConsoleApplication2.exe”已退出,回傳值為 0 (0x0)。
uj5u.com熱心網友回復:
#include <iostream>
#include <string>
using namespace std;
#define MAXLEN 200
char *strsub(char *s, int i, int len)
{
int j, slen;
static char t[MAXLEN];
slen = strlen(s);
if (i>slen || i<1 || len<0 || len>slen - i + 1) {
cout << "錯誤,請重新輸入!!!";
return 0;
}
for (j = 0; j < len; j++)
t[j] = s[i + j - 1];
t[j] = '\0';
return (t);
}
int main()
{
int len, i;
char s[MAXLEN];
char *p;
cout << "請輸入原字串:";
cin >> s;
cout << "請輸入子串的起始位置:";
cin >> i;
cout << "請輸入子串的長度:";
cin >> len;
p = strsub(s, i, len);
cout << p << endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/101599.html
標籤:基礎類
