简体   繁体   中英

Pass char pointer array to function

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

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

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

    }

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, ie 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 , ie &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 *));

You're sending states which is an array of pointers . So you need to send the base address of that array to the function

sort(*states);

And then receive it in an array of pointer only

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

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

}

Hardcoding the size if also not a good idea, so better add another parameter size to the function call.

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

and later use that to iterate over the array

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM