I'm trying to pass an initialized char pointer array to a function. I can't seem to figure out why the function will only print out the numeric digits of each element in the array.
Does anyone know how I can print each string element from the passed in pointer array?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
void sort(char *);
int main()
{
char *states[4] = {"Florida", "Oregon", "California", "Georgia"};
sort(*states);
return 0;
}
void sort(char *states)
{
int x;
for (x = 0; x < 4; x++) {
printf("\nState: %d\n", states[x]); //only this will compile
//printf("\nState: %s\n", states[x]); //need to print this.
}
}
Your sort
function must accept the array of pointers if you wish to print the contents of the array.
void sort (char *states[], size_t num_states) {
int x;
for (x = 0; x < num_states; ++x) {
printf("\nState: %s\n", states[x]); /* Note %s instead of %d */
}
}
And, you must pass the array to the function.
sort(states, 4);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments