Pass char pointer array to function

21,542

Solution 1

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);

Solution 2

You need to parse an array of pointers to char to sort (instead of just pointer to char).

As jhx pointed out, you need to pass the size of the array as well. You can use sizeof so as to not hard-coding 4. Also omit the array size when initialize it.

void sort( char *states[], int arr_size )
{
    int x;

    for (x = 0; x < arr_size; x++) 
    {
        printf( "\nState: %s\n", states[x] );
    }
}

int main()
{
    char *states[] = {"Florida", "Oregon", "California", "Georgia"};     // array of pointers to char

    sort( states, sizeof( states ) / sizeof( char * ) );

    return 0;
}

Solution 3

You need to pass the char pointer array to the function:

   #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>

    void sort(char *args[], int n);

    int main()
    {
       char *states[4] = {"Florida", "Oregon", "California", "Georgia"};

       sort(states, 4);

       return 0;
    }

    void sort(char *states[], const int N)
    {
       int x;

       for (x = 0; x < N; x++) {
          printf("\nState: %s\n", states[x]); 
       }

    }

Solution 4

The reason that only numeric values you are getting is that only pointer to first element of string states[0] of the array states is passed, i.e. you are passing &states[0][0]. So, the statement

printf("\nState: %d\n", states[x]);  

will only print the numeric value of first 4 characters of string "Florida".

You need to pass the pointer to first element of array states, i.e. &states[0].
This can be done by changing the declarator of function sort to

void sort(char **, size_t size); // You need to pass the size of array too. 

and call it as

sort(states, sizeof(states)/sizeof(char *));
Share:
21,542
Andrew Hummel
Author by

Andrew Hummel

Updated on July 09, 2022

Comments

  • Andrew Hummel
    Andrew Hummel almost 2 years

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