Pass char pointer array to function

Andrew Hummel

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.
   }

}
jxh

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass char pointer/array to a function

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

Pass char pointer to function in C

From Dev

Passing a char pointer array to a function

From Dev

Passing pointer of char array to function

From Dev

Realloc pointer to a char array in function

From Dev

Realloc pointer to a char array in function

From Dev

simply accessing char array in a function by char pointer

From Dev

pass a void pointer to a function with const char * const

From Dev

Alternative way to pass a char pointer to a function

From Dev

pass a void pointer to a function with const char * const

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Char pointer to char array

From Dev

Pointer to char into char array

From Dev

Pass pointer of array to function that sorts numbers

From Dev

how do i pass an array with a pointer into a function?

From Dev

how do i pass an array with a pointer into a function?

From Dev

Pass a pointer of an array to a function to use in printf` in C

From Dev

How to pass pointer to array of byte to function?

From Dev

How to pass an array of char's to a function in C

From Dev

Can't pass a char array to a function

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass in function a multidimensional char array in C?

From Dev

pass and modify char array[][] in function C

From Dev

Unable to pass an address of array of type char *[2] to function taking char ***

From Dev

char pointer not the same to char array?

From Dev

C *char pointer to char array

From Dev

Char pointer to anonymous char array

Related Related

  1. 1

    Pass char pointer/array to a function

  2. 2

    How to pass a float array to the function that has unsigned char pointer as an argument?

  3. 3

    Pass char pointer to function in C

  4. 4

    Passing a char pointer array to a function

  5. 5

    Passing pointer of char array to function

  6. 6

    Realloc pointer to a char array in function

  7. 7

    Realloc pointer to a char array in function

  8. 8

    simply accessing char array in a function by char pointer

  9. 9

    pass a void pointer to a function with const char * const

  10. 10

    Alternative way to pass a char pointer to a function

  11. 11

    pass a void pointer to a function with const char * const

  12. 12

    Passing a pointer to a char array as an argument to a function - C

  13. 13

    Passing a pointer to a char array as an argument to a function - C

  14. 14

    Char pointer to char array

  15. 15

    Pointer to char into char array

  16. 16

    Pass pointer of array to function that sorts numbers

  17. 17

    how do i pass an array with a pointer into a function?

  18. 18

    how do i pass an array with a pointer into a function?

  19. 19

    Pass a pointer of an array to a function to use in printf` in C

  20. 20

    How to pass pointer to array of byte to function?

  21. 21

    How to pass an array of char's to a function in C

  22. 22

    Can't pass a char array to a function

  23. 23

    How to pass in function a multidimensional char array in C?

  24. 24

    How to pass in function a multidimensional char array in C?

  25. 25

    pass and modify char array[][] in function C

  26. 26

    Unable to pass an address of array of type char *[2] to function taking char ***

  27. 27

    char pointer not the same to char array?

  28. 28

    C *char pointer to char array

  29. 29

    Char pointer to anonymous char array

HotTag

Archive