我創建交換(使用couts來測試它,然後在我的實際程序中實現它)函數與指針,我不完全確定爲什麼我得到此分段錯誤時我運行它。有任何想法嗎?獲取交換功能的分段錯誤11
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char * initializeWord(int length);
void swap(char *a, char *b);
//void scrambleWord(char *word, int size);
int main()
{
int length;
char *word, *x, *y;
cout << endl << "Welcome to Word Scrambler!" << endl << endl;
cout << "How many letters will your word have?" << endl << endl;
cin >> length;
getchar();
cout << endl << "Please input a word that contains " << length << " many characters." << endl << endl;
word = initializeWord(length);
cout << endl;
cout << "The word you entered was: " << word << endl << endl;
swap(x,y);
delete[] word;
return 0;
}
char * initializeWord(int length)
{
//initialization of char array
char *cArray = new char[length];
//user's word
cin >> cArray;
getchar();
return cArray;
delete[] cArray;
}
void swap(char *a, char *b)
{
cout << "First values:" << endl << a << endl << b << endl;
char *temp = a;
a = b;
b = temp;
cout << "Second values:" << endl << a << endl << b << endl;
}
您從未將'x'和'y'設置爲任何東西... – GigaRohan
不要在C++中編寫C代碼。使用引用和'std :: vector'。 – Alexander
「return」後不能'刪除';你返回後不能做任何事 – pinkfloydx33