Public Group

# Pointertrouble

This topic is 2522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I am currently trying to read and analyze a text (or multiple, one at a time). The purpose is to read a text, find the lettercount, calculate the probability of each of the letters. I use pointers to remove unused variables from completed readings and to create new ones for the next reading.

The code is as following

 // Huffman Encoder v1.0 - Begun 06/01/2012, updated 06/01/2012 // Drag and drop a text file to be encoded and encode it. // Mikkel S. Bugge #include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char *argv[]) { // Check to see if text has been dragged onto if(argc < 2) { cout << "Drag file onto .exe file to encode" << endl; cin.get(); } else { int read = 1; // Number of read files while(read != argc) { double **d_arLetters; double **d_arLettersProb; string line; // Text d_arLetters = new double *[256]; // Letters and usecount d_arLettersProb = new double *[256]; // Probability // Reset value and prob of letters for(int j = 0;j < 256;j++) { d_arLetters[j] = 0; d_arLettersProb[j] = 0; } float * f_letterCount = 0; // Pool (Number) of letters for(int i=1;i<argc;i++) { cout << "Encoding file \"" << argv << "\"\n" << endl; ifstream fi(argv); while(getline(fi,line)) // Read line till end of file; { f_letterCount += line.length(); // Problem for(int k=0; k < *f_letterCount; k++) // Problem!? { d_arLetters[int(line[k])]++; // Add letter to letterpool. } // Calculate proability for(int k = 0; k < 256; k++) { *d_arLettersProb[k] = *d_arLetters[k] / *f_letterCount; } cout << line << endl; } cout << "Probability of e: " << *d_arLettersProb[101]; } read++; } } cin.get(); return 0; } 

The output given is generally to see if its working. The problem is, that crashes around the for loop with the comment "Problem!?". Its a long time since I have worked with pointers, so I cant remember much of it. Some of the code may be rubbish, if not all of it. Though, its not finished yet. I hope someone can help me fix the error.

Regards,
Boooke

##### Share on other sites
Most probably f_letterCount isn't a valid pointer, because you initialise it to zero and don't set it to anything later. I think you want to make f_letterCount a normal float variable, and get rid of the * in the line commented "Problem!?".

##### Share on other sites
Hey again. Thank you for the reply. I have already tried your suggestion (Which i did first, actually). It gives me a problem later on, when I try to use it later on in the line,

 ... *d_arLettersProb[k] = *d_arLetters[k] / *f_letterCount; ... 

I can try show the error later on, but right now, I dont have the code around.

Regards,
Boooke

##### Share on other sites
Alright, you seem to be using too many pointers to things. Get rid of one * from the lines
double **d_arLetters;
double **d_arLettersProb;
and get rid of all the other occurences of * in the body of the main function

##### Share on other sites
You should be using static arrays (e.g. double letterProbability[256]) rather than pointers to dynamically allocated arrays.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×