I’m trying to read individual characters from a string constant passed into a function and put them into an array. The end result i want is an a string array that is the same string as the string constant. This is my code:
for(int i = 0; i < string_length; i++)
{
sscanf(string, "%c", &array[i]);
}
When string is “string” all i get is a string_length sized array with every value being an s.
Any ideas?
sscanf cannot remember your index positioning and automatically increment pointers. So, effectively, it ends up reading the first character each time you call it in the loop.
Try as below
Additional Note:
You can as well use strcpy or memcpy to copy the string into a character array. I hope you have a good enough reason for using sscanf for this.