![]() |
|
#1
|
|||
|
|||
array of pointers to stringsHi!
I have an array of pointers to strings (char *name[maxnum]). I add elements to the array and output each element right after to check their content and it is okay. char[counter] = value; cout << char[counter] << endl; However, at a later point in the code I output all the elements, and they are all equal to element the last element (maxnum). :-( for (int i=0;i<=maxnum;i++) { cout << char[i] << endl; } I do a similar process with an array of doubles (double [maxnum]) and the process above works fine. Note that I do not want to use strings. Can you help figure out what is wrong when I use the array of pointers to store strings? Thanks, Michelle |
|
#2
|
|||
|
|||
|
Quote:
Here's the drill: Instead of telling us what you did (or what you think you did, or what you tried to do), show us what you did (post the code). Tell us: 1. What you expected to get. 2. What you got and don't understand. 3. What compiler? What operating system? (Sometimes it matters.) Regards, Dave |
|
#3
|
|||
|
|||
|
Quote:
Hi Dave! My code is below: CPP / C++ / C Code:
The statements/blocks in question are: char *validEmployeeName[maxEmployees]; validEmployeeName[val_employee_counter] = employeeName; for (int i=0; i<=(val_employee_counter-1); i++) { cout << i+1 << ". " << setw(40) << left << validEmployeeName[i] << "$" << setw(14) << right << validwklyPay[i] << endl; } Thanks! Michelle |
|
#4
|
|||
|
|||
|
Quote:
You are using arrays of char. You want to use strcpy() to copy the names: Your program does this: CPP / C++ / C Code:
This makes the pointer point to the employeName array, whose content changes for each entry, and finally it contains the last employee name that was entered. In other words, all of your validEmployeeName[] pointers end up pointing to the employeeName array, therefore the output will show them to be the same. To get the name into the pointer array element that you just obtained from new, use strcpy. CPP / C++ / C Code:
(Also setting validEmployeeName[...] equal to something other than the value given by new means that the memory can never be freed. This is a Bad Thing. It is the programmer's responsibility to make sure that for every memory block obtained by new it is returned to the operating system by a delete before exiting. This is important!) Regards, Dave Last edited by davekw7x : 20-Jan-2005 at 21:12.
|
|
#5
|
|||
|
|||
|
Hi! Thanks! This worked. Now it makes sense!
My only question is: how do I use delete to free up the memory for every pointer in the array? Within the 'C' option, I tried a single: delete [] validEmployeeName; but it did not work. :-( Do I need a loop? Thanks, Michelle |
|
#6
|
|||
|
|||
|
Ok! Got it:
for (int ctr=0; ctr<=(val_employee_counter - 1); ctr++){ delete [] validEmployeeName[ctr]; } Finally getting the hang of it Michelle |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Function and Array (w/ reference variables) question | brookeville | C++ Forum | 15 | 07-Dec-2004 01:11 |
| insert sort | saphir55 | C Programming Language | 4 | 06-Dec-2004 14:00 |
| Using pointers and strings | dilmv | C++ Forum | 16 | 04-Dec-2004 08:48 |
| Speed up C++ code about 3d array! | Truong Son | C++ Forum | 0 | 16-Mar-2004 21:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The