| | |
Declaration of dynamic pointer array puzzle.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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:
<< 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.
<< 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.
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:
C Syntax (Toggle Plain Text)
int textlinecount(FILE* fp) { int counted = 0; while(!feof(fp)) { if(getc(fp)=='\n'||feof(fp)) counted++; } rewind(fp);//Return to start of file for use by textreadandupdate. return counted; }
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.
C Syntax (Toggle Plain Text)
BOOL TextReadAndUpdate(FILE* filetoread) { char str[50]; while(!feof(filetoread)) { for(int i=0;i<50;i++) { str[i]=getc(filetoread); if(str[i]=='\n'||feof(filetoread)) { if(feof(filetoread)&&(i<50)) { while(i<50) { str[i] = ' '; i++; } } txtContents = (CString)str; UpdateAllViews(NULL); break; } } } return TRUE; }
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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.
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.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Pointer to array (C++)
- Dynamic memory for an array of objects (C++)
- pointer and array arithmetic (C)
- Help with copying an array into a struct (C)
- structures containing a pointer to an array (C++)
- struct dynamic 2d array alloc (C)
Other Threads in the C Forum
- Previous Thread: how to create a vector of type structure
- Next Thread: easy question in C...
Views: 4212 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi





