Print char pointer char array with printf?

I am trying to type a whole word instead of the first letter. I could do this with a loop, but I thought there was a better way. I searched for the answers and saw the answers that they changed% S to% c, but I am already using% c, since it is an array of characters.

char* words[] = {"my", "word", "list"}; printf("The word: %c",*words[2]); Results: The word: l 
+4
source share
2 answers

You need to use %s , a format used specifically for character arrays with a null character (i.e. C strings). You do not cast the element of the array when you pass it to printf , for example:

 printf("The word: %s\n", words[2]); 
+4
source

The problem is that you dereferenced twice. [2] in * words [2] the differences between the words [] and the "list", and then * are played out a second time from the "list" to "l". Delete * and voila.

 char* words[] = {"my", "word", "list"}; printf("The word: %s", words[2]); 
+7
source

Source: https://habr.com/ru/post/1444934/


All Articles