01/10/2018, 14:51

Em viết hàm strncopy nhưng khi lúc test trong hàm main thì có lỗi vùng nhớ

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

using namespace std;

int StringLength(char *s)
{
	int i = 0;
	while (*(s + i) != '')
	{
		i++;
	}
	return i;
}

char* StringNCopy(char* dest, char* src, int numChar)
{
	int lengthSrc = StringLength(src);
	if (numChar > lengthSrc)
		numChar = lengthSrc;
	dest = new char[numChar];
	int i = 0;
	while (i<numChar)
	{
		dest[i] = src[i];
		i++;
	}
	dest[numChar] = '';
	return dest;
}
char* StringCopy(char* dest, char* src)
{
	int LengthDest = StringLength(dest);
	int LengthSrc = StringLength(src);
	return 0;
}

void OutputString(char* s, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << *(s + i);
	}
}
void main()
{
	char* src=new char[];
	char* dest = new char[];
	int numChar;
	cout << "Enter source string :";
	gets(src);
	cout << "Enter number of character :";
	cin >> numChar;
	dest = StringNCopy(dest,src,numChar);
	OutputString(dest, numChar);

	delete[] dest;
	delete[] src;
	system("pause");
}
rogp10 viết 16:53 ngày 01/10/2018

Viết thế này sai 100% dù có đúng thì chắc đc vài char là cùng.

`char* src=new char[];
	char* dest = new char[];`
Thanh Tùng viết 17:05 ngày 01/10/2018

vậy làm sao đây anh

Bài liên quan
0