I was trying to do a question in which I was to ask for input from the user and store that input in an array many times (so I used a loop). However, even though I declared and initialized a new char array in each iteration and store in a char * array, all char array(I was trying to use them as strings in this case) stored in the char * array seem to have the same memory address, even if I declare a new one each time! How is this happening?
Great thanks!!
Below is a simplified snippet of codes to demonstrate the behaviour I am talking about. When it is run, all outputs were just 9. The 'ask for input' is simplified to inserting index-related values in the array
int main(int argc, char** argv){
char* batsman[10];
for(int i = 0; i < 10; i++){
char newString[2];
newString[0] = i + 48;
newString[1] = '\0';
batsman[i] = newString;
}
for(int i = 0; i < 10; i++){
printf("%s\n",batsman[i]);
}
return 0;
}