Trying to revise for a coding exam and I came across this slide. Can any of you help me to understand how to print the strings from a pointer to array? Here is the code sample:
#include <stdio.h>
int main(void)
{
size_t i;
const char *suit[4] = { "Hearts", "Diammonds", "Clubs", "Spades" };
printf("The strings in array const char*suit[4] is:\n\n");
for (i = 0; i < 5; i++)
{
printf("const char *suit[%u] is: %s\n", i, suit[i]);
}
}
When I print, I get this output:
The strings in array const char*suit[4] is:
const char *suit[0] is: Hearts
const char *suit[1] is: Diammonds
const char *suit[2] is: Clubs
const char *suit[3] is: Spades
However, it came with this error as well. "Exception thrown at 0x7AAD170C (ucrtbased.dll) in Pointer Practice.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC."
Why is there an error? How do I print the same output, but without an error?
Thank you in advance guys!
printf("const char *suit[%u] is: %s\n", i, *suit[i]);This can't be the real code.*suit[i]is acharand printing it with format%scan never result in those strings you quote.