获取交换功能的分段错误11
Getting a segmentation fault 11 for swap function
我正在创建一个互换(在我的实际程序中实现它之前使用couts对其进行测试),我不确定为什么当我在我运行。有什么想法吗?
#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;
}
cin>> carray;该行将用户输入为您的数组的地址。
您可能想要:
char* initializeWord(int length)
{
char* cArray = new char[length];
for(int i = 0; i < length; ++i)
{
cin >> cArray[i]
}
...
}
或仅使用字符串。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)