pointers and chars

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 49
Reputation: helixkod is an unknown quantity at this point 
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

pointers and chars

 
0
  #1
Nov 22nd, 2007
Heres my current code:
  1. const int SIZE = 100; //constant 100
  2. const int COL = 30; //number of columns
  3. int testScores[SIZE]; //test score array
  4. char students[SIZE][COL]; //student names array
  5. int numTests; //the number of tests
  6. char *namePtr; //char pointer
  7. int *numPtr; //number pointer
  8. int count; //counter
  9.  
  10. numPtr = testScores; //sets the pointer to testScores
  11. namePtr = students; //set the pointer to students

can pointers not be set to a char?
if that is the case should i just use an array?

I was told to use pointers whenever possible, i just don't know if a char pointer is possible.
Last edited by helixkod; Nov 22nd, 2007 at 7:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: pointers and chars

 
0
  #2
Nov 22nd, 2007
> I was told to use pointers whenever possible
In C++ you should avoid both.

> i just don't know if a char pointer is possible.
Did you try compiling your code above? Did it complain about a char * ?

You can make a pointer to anything.

You'd be better off, though, using a std::string:
  1. #include <string>
  2. using namespace std;
  3.  
  4. string students[ SIZE ];
Of course, you have to keep track of how many students you actually have, and you can never have more than SIZE students.

You'd be best off, then, using a std::vector or std::deque:
  1. #include <string>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<string> students;
  6.  
  7. students.push_back( "Jayne" );
  8. students.push_back( "Ronnie" );
  9. students.push_back( "Hannah" );
  10.  
  11. cout << "The students' names are:\n";
  12. for (int i = 0; i < students.size(); i++)
  13. cout << student[ i ] << endl;

Hope this helps.
Last edited by Duoas; Nov 22nd, 2007 at 8:10 pm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC