•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,461 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,796 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2361 | Replies: 14
![]() |
•
•
Join Date: Apr 2005
Posts: 30
Reputation:
Rep Power: 4
Solved Threads: 0
HAving trouble with my pointer arithmetic, any ideas appreciated
My orgional code
converted but wrong
my struct
if you want the whole code let me know
My orgional code
Activity p[51];
for ( int i = 1; i <= n; ++i ) {
// Read the activity records.
pert >> p[i].i >> p[i].j >> p[i].pt
>> p[i].prt >> p[i].ot;
pert.getline(p[i].desc,21);Activity **p = new Activity*[51];
for ( i = 1; i <= n; ++i ) {
if (p[i].i == NULL)
break;
pert >> (*p+i)->i >> *(p+i)->j >> *(p+i)->pt
>> *(p+i)->prt >> *(p+i)->ot;
pert.getline(*(p+i)->desc,21);
}my struct
struct Activity {
int i, // Beginning node number.
j; // Ending node number.
float pt, // Pessimistic time.
prt, // Most probable time.
ot; // Optimistic time.
char desc[21]; // Activity description.
};if you want the whole code let me know
•
•
Join Date: Apr 2005
Posts: 30
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by winbatch
Can you indicate what the 'problem' is? Is it when you attempt to print the contents of the array?
I am trying to read each line as a new activity and then have all the activities in an array.
Was this what you were trying to do?
Activity **p = new Activity*[51];
for ( int i = 1; i <= n; ++i ) {
// Read the activity records.
pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt
>> (*(p+i))->prt >> (*(p+i))->ot;
pert.getline((*(p+i))->desc,21);
}•
•
Join Date: Apr 2005
Posts: 30
Reputation:
Rep Power: 4
Solved Threads: 0
•
•
•
•
Originally Posted by Dogtree
Was this what you were trying to do?
Activity **p = new Activity*[51]; for ( int i = 1; i <= n; ++i ) { // Read the activity records. pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt >> (*(p+i))->prt >> (*(p+i))->ot; pert.getline((*(p+i))->desc,21); }
This is how i implemented it
for ( i = 1; i <= n; ++i ) {
cout<< "test5" << endl;
if ((*(p+i))->i == NULL){
cout<< "test4" << endl;
break;
}
cout<< "test4" << endl;
// Read the activity records.
pert >> (*(p+i))->i >> (*(p+i))->j >> (*(p+i))->pt
>> (*(p+i))->prt >> (*(p+i))->ot;
pert.getline((*(p+i))->desc,21);
cout << (*(p+i))->i << endl;
} Either n is bigger than 50, which is the last accessible index in your array, or you aren't allocating memory to each Activity pointer in the array. You probably want to allocate the initial memory like this:
Then you release the memory like this:
Unless you do this, you'll have to initialize each pointer to an existing object before you try to dereference it.
Activity **p = new Activity*[51]; for (int i = 0; i < 51; i++) p[i] = new Activity;
for (int i = 0; i < 51; i++) delete [] p[i]; delete p;
•
•
Join Date: Apr 2005
Posts: 30
Reputation:
Rep Power: 4
Solved Threads: 0
ok i adjusted some things as that doesnt seem to be what i wanted, but my code still wont read in the text file and then allow me to print it out as needed.
the first line of the text file reads
1 2 1 2 4 ORD.FIXTURES
but when i do
cout << (p+i)->i << endl;
i get a 0
and the final out put is just garbage ( if you want the final out put code i can post it but its large)
Activity *p = new Activity [51];
for (i = 1; i <= n; ++i )
{
cout<< "test5" << endl;
if ((p+i)->i == NULL)
{
cout<< "test6" << endl;
break;
}
cout<< "test7" << endl;
// Read the activity records.
pert >> (p+i)->i >> (p+i)->j >> (p+i)->pt
>> (p+i)->prt >> (p+i)->ot;
pert.getline((p+i)->desc,21);
cout << (p+i)->i << endl;
}1 2 1 2 4 ORD.FIXTURES
but when i do
cout << (p+i)->i << endl;
i get a 0
and the final out put is just garbage ( if you want the final out put code i can post it but its large)
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Pointers (archived tutorial) (C++)
- Problem About Pointers (C++)
- New to C++ Pointers and need help with identifying where the errors are (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why can't I use Pointers to point to a Enumurated Constant (C++)
Other Threads in the C++ Forum
- Previous Thread: ClassTemplate With LinkedList??
- Next Thread: Quick, Insertion, and Partition


Linear Mode