I am trying to use dynamic memory for this project. I am getting a seg fault but I cannot figure out what I am doing incorrectly. Can anyone point to where my mistake is? The file seems to read in correctly...but im assuming the fault is a rogue pointer..help!

I am just trying to read in "heart two 2" to "spade ace 11" in from a file, all words seperated by a space. my program worked before using dynamic memory..

```
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

//global constant(s)
const int maxCards = 52;

//Structs
struct card 
{
    char *suit;
    char *rank;
    int cvalue;
    char location;
};

void readDeck(card* deckPtr);
void cardsInit(char *finNameP,card *deckPtr);

//program
int main()
{
  card *deckPtr = new card[52];

  char *finNameP = new char[13];
  strcopy(finNameP,"cardFile.txt");
  cardsInit(finNameP,deckPtr); // function i wrote that works
  readDeck(deckPtr); //simply reads the deck from &deckPtr[0] -> &deck[51]
  delete [] finNameP;
}

void cardsInit(char *finNameP, card *deckPtr)
{
  //set up card file to be read in
  ifstream fin;
  cout << "Please enter file name...(cardFile.txt)" << endl;;
  cin >> *finNameP;
  fin.open(finNameP);

  //create pointer and set initial value
  card *deckHome = deckPtr;

  for(int i=0;i<52;i++)
  {
    (*deckPtr).suit = new char[9];
    (*deckPtr).rank = new char[9];
    deckPtr++;
  }
  deckPtr = deckHome;
  //check if cardFile.txt opens correctly
  if(!fin.good())
  {
    cout << "Error with card file" << endl;  
  }
  else
  {
    while(fin.good())
    {
      for(deckPtr = &deckPtr[0]; deckPtr < &deckPtr[maxCards];deckPtr++)
      {
        fin >> (*deckPtr).suit;
        fin >> (*deckPtr).rank;
        fin >> (*deckPtr).cvalue;
      }   
    }    
  } 

  fin.close();    
  delete []finNameP;
  delete [] (*deckPtr).suit;
  delete [] (*deckPtr).rank;
}

```
['https://Stackoverflow.com/questions/26009316', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4073150/']
This is a really ancient way to program. Instead of using `new`, use `std::string` or `std::vector<char>`. Those also use dynamic memory but they make it much harder for you to accidentally cause memory allocation bugs.

The first problem comes here:

```
cin >> *finNameP; 

```

Since `finNameP` has type `char *`, then `*finNameP` has type `char`. So this instruction reads a single character. Then you go onto do `fin.open(finNameP);` which causes undefined behaviour because there is no string in `finNameP`.

The simplest fix is to make `finNameP` be a `std::string`. Note that doing `cin >> finNameP` (without changing the type) would compile, however it is a bad idea because there is no buffer overflow protection. You could write `cin >> setw(12) >> finNameP;` but that is still substantially worse than using a string.
`deckPtr < &deckPtr[maxCards]` is always true, the for loop runs forever.