Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 811
Andrew Hummel Avatar asked Dec 21 '15 19:12

Andrew Hummel


People also ask

How do you pass a char array to a function?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

Can we pass array of pointers to function?

C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index.


4 Answers

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);
like image 198
jxh Avatar answered Oct 06 '22 01:10

jxh


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;
}
like image 33
artm Avatar answered Oct 05 '22 23:10

artm


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

    }
like image 43
seleciii44 Avatar answered Oct 06 '22 01:10

seleciii44


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 *));
like image 34
haccks Avatar answered Oct 06 '22 00:10

haccks