0

I'm creating a swap (using couts to test it before implement it in my actual program) function with pointers and I'm not entirely sure why I'm getting this segmentation fault when I run it. Any ideas?

#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;
}
Parker
  • 3
  • 2

1 Answers1

2

cin >> cArray; This line is setting the users input as the address of your array.

You probably want:

char* initializeWord(int length)
{
   char* cArray = new char[length];

   for(int i = 0; i < length; ++i)
   {
        cin >> cArray[i]
   }
   ...
}

Or just use strings.

JGroven
  • 613
  • 1
  • 9
  • 14
  • This is what I want it to do for now, it if causes issues later I'll change it. But, all I'm trying to do right now is test whether my swap function works correctly (which it doesn't). I've tried setting *x and *y to chars 'x' and 'y' and this doesn't work either... Kinda stuck – Parker Mar 01 '17 at 01:27
  • @Parker You mean to tell me that you expect your user to input a valid address? I think you want them to input a word, instead. You are writing to the variable that holds the address of the array, not to the array itself. – JGroven Mar 01 '17 at 01:28
  • Hmm, you're right that I don't want to do that, but that part was working when I was running it. The goal is to take the word inputed and have each char from that word in the array. – Parker Mar 01 '17 at 01:31
  • Ok this works too. Pretty sure this is what I should've tried first. I have to implement two other functions (not looking for answers), but am I deleting cArray too early if it is to be used later? – Parker Mar 01 '17 at 01:39
  • You aren't deleting cArray at all, in fact. You are returning before that can happen and ending the function prematurely. – JGroven Mar 01 '17 at 01:44
  • Right, I've fixed that and put delete[] cArray before the return statement. Same question still stands. – Parker Mar 01 '17 at 01:47
  • Don't delete it if you still want to use it. – JGroven Mar 01 '17 at 01:50
  • Makes sense haha. Ok thank you so much for the help, I'll see if I can't finish the assignment now. – Parker Mar 01 '17 at 01:52