User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2005
Posts: 30
Reputation: sinrtb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Trying to use pointers

  #1  
May 16th, 2005
HAving trouble with my pointer arithmetic, any ideas appreciated
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);
converted but wrong
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 458
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Trying to use pointers

  #2  
May 16th, 2005
Why are you starting your array at 1 instead of 0? Also, how big is n?
Reply With Quote  
Join Date: Apr 2005
Posts: 30
Reputation: sinrtb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: Trying to use pointers

  #3  
May 16th, 2005
started the array at 1 becaused that was a requirement of the assigmnent. n is the number of activities and in the test case its 11. I'm not sure i can dynamically allocate for n without alot of copying.
Reply With Quote  
Join Date: Feb 2005
Posts: 458
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Trying to use pointers

  #4  
May 16th, 2005
Can you indicate what the 'problem' is? Is it when you attempt to print the contents of the array?
Reply With Quote  
Join Date: Apr 2005
Posts: 30
Reputation: sinrtb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: Trying to use pointers

  #5  
May 16th, 2005
Originally Posted by winbatch
Can you indicate what the 'problem' is? Is it when you attempt to print the contents of the array?
I can't seem to figure out the syntax of a pointer to an array of pointers to structs. In the origional code it works fine but when i try to transform it to pointer arithmetic the syntax is wrong.
I am trying to read each line as a new activity and then have all the activities in an array.
Reply With Quote  
Join Date: Feb 2005
Posts: 458
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Trying to use pointers

  #6  
May 16th, 2005
Post the code where you are trying to utilize/print out the array to show where the problem manifests itself..
Reply With Quote  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Trying to use pointers

  #7  
May 16th, 2005
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);
}
Reply With Quote  
Join Date: Apr 2005
Posts: 30
Reputation: sinrtb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: Trying to use pointers

  #8  
May 17th, 2005
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;
	}
Can you tell me why im getting a seg fault?
Reply With Quote  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Trying to use pointers

  #9  
May 17th, 2005
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:
Activity **p = new Activity*[51];

for (int i = 0; i < 51; i++)
  p[i] = new Activity;
Then you release the memory like this:
for (int i = 0; i < 51; i++)
  delete [] p[i];
delete p;
Unless you do this, you'll have to initialize each pointer to an existing object before you try to dereference it.
Reply With Quote  
Join Date: Apr 2005
Posts: 30
Reputation: sinrtb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: Trying to use pointers

  #10  
May 18th, 2005
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.
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;
	}
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)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC