| | |
pointers and chars
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 1
Heres my current code:
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.
C++ Syntax (Toggle Plain Text)
const int SIZE = 100; //constant 100 const int COL = 30; //number of columns int testScores[SIZE]; //test score array char students[SIZE][COL]; //student names array int numTests; //the number of tests char *namePtr; //char pointer int *numPtr; //number pointer int count; //counter numPtr = testScores; //sets the pointer to testScores 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.
> 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
You can make a pointer to anything.
You'd be better off, though, using a std::string:
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:
Hope this helps.
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:
C++ Syntax (Toggle Plain Text)
#include <string> using namespace std; string students[ SIZE ];
You'd be best off, then, using a std::vector or std::deque:
C++ Syntax (Toggle Plain Text)
#include <string> #include <vector> using namespace std; vector<string> students; students.push_back( "Jayne" ); students.push_back( "Ronnie" ); students.push_back( "Hannah" ); cout << "The students' names are:\n"; for (int i = 0; i < students.size(); i++) cout << student[ i ] << endl;
Hope this helps.
Last edited by Duoas; Nov 22nd, 2007 at 8:10 pm.
![]() |
Similar Threads
- Purpose of Pointers? (C++)
- Concatenation of strings without string.h (C++)
- Calculating the cardinality of an array (C++)
- structure bit fields (C)
- arrays of pointers help (C)
Other Threads in the C++ Forum
- Previous Thread: strtoul() function use
- Next Thread: char array error
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






