Declaration of dynamic pointer array puzzle.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Declaration of dynamic pointer array puzzle.

 
0
  #1
Jul 18th, 2005
Hello, everyone.

Today, I'm working on giving myself a headache.

The reason is simple. I'm rusty on, or maybe ignorant of, dynamic declaration of arrays.

What I'm doing is MFC related, but I've got the MFC questions ironed out for the most part. Now, I've got to make a good storage system for CStrings.
I want to use an array of pointers to CStrings. The problem is, I don't know how many pointers I'll need until after I've opened a text file.
But wait, it's not that simple. I've written an int-returning '\n' counter, and it goes-a-like this:

  1. int textlinecount(FILE* fp)
  2. {
  3. int counted = 0;
  4.  
  5. while(!feof(fp))
  6. {
  7. if(getc(fp)=='\n'||feof(fp))
  8. counted++;
  9. }
  10. rewind(fp);//Return to start of file for use by textreadandupdate.
  11. return counted;
  12. }
<< moderator edit: added [code][/code] tags -- learn to do so yourself >>

That should work just fine. What is wrong, though, is that this, and the CString pointer array, are part of the same class declared at startup, before a file is loaded.
Then, I use this to read in the file information.

  1. BOOL TextReadAndUpdate(FILE* filetoread)
  2. {
  3. char str[50];
  4. while(!feof(filetoread))
  5. {
  6. for(int i=0;i<50;i++)
  7. {
  8. str[i]=getc(filetoread);
  9.  
  10. if(str[i]=='\n'||feof(filetoread))
  11. {
  12. if(feof(filetoread)&&(i<50))
  13. {
  14. while(i<50)
  15. {
  16. str[i] = ' ';
  17. i++;
  18. }
  19. }
  20. txtContents = (CString)str;
  21. UpdateAllViews(NULL);
  22. break;
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29. return TRUE;
  30. }
<< moderator edit: added [code][/code] tags >>

Given the nature of MFC and not wanting to make the information array exist outside this 'document' class, how do I dynamically allocate a number of pointers after the class is... er... loaded..declared..initialized... by the program?

I think I should include 'CString *lineptrs;' in the header file, but I'm not sure how to continue after that.

Thank you for your time.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Declaration of dynamic pointer array puzzle.

 
0
  #2
Jul 18th, 2005
The solution that comes to mind is a doubly-linked list.

That means trouble.
Lessee... head, tail, frame transverse head, frame transverse tail... yipeeskipee. There go my hopes of easy pointer arithmetic to control the view...



Upon yet further reflection:
This is the best way to implement the program, as it allows me to add more functionality that is going to be required in future updates with very little fuss.

No more whining.

Status of Puzzle:
Resolved.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4212 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC