I'm currently studying C and I'm trying to just print the contents of a string array. I'm using pNames
to point to the first char pointer and iterating from there.
A more proper approach would use this pointer, get a char* each time and use printf("%s", pNames[i])
to print a whole string. However, I thought I would try to print it character-by-character inside each string, as follows:
#include <stdio.h>
int main(int argc, char *argv[])
{
char *names[] = {
"John", "Mona",
"Lisa", "Frank"
};
char **pNames = names;
char *pArr;
int i = 0;
while(i < 4) {
pArr = pNames[i];
while(*pArr != '\0') {
printf("%c\n", *(pArr++));
}
printf("\n");
i++;
}
return 0;
}
This code kind of works (prints each letter and then new line). How would you make it better?