I'm writing code that inputs a file and reads in each word as a separate char* like this:
char label[8];
char type[5];
char value[6];
while (!input.eof()) {
input >> label;
input >> type;
input >> value;
storeSymbols(label, type, value);
}
Then I set it to an element of a char* array like so:
void storeSymbols(char* lab, char* type, char* val) {
labels[symCount] = lab;
types[symCount] = type;
values[symCount] = val;
symCount++;
}
However, when I print out all of the elements of the char* array, all of the elements are the same as the last element retrieved from the file. For example, if there were three labels in the document, defined as "one", "two", and "three" then at the end of the first loop, the array will contain "one", the second time it will contain "two" and "two" and the third time it will contain "three" "three" and "three". It also loops through an extra time, adding the last element to the array another time, making it four "three" elements in the array. Does anyone have any idea why this is happening?
I've also tested this where I convert value to an int and add it to an int array and it works fine.