9

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

}
3
  • You pass a pointer to Florida to the function, and then print out the numbers corresponding to the first four letters of that string. Wasn't that what you intended? Commented Dec 21, 2015 at 19:25
  • 2
    sort(states); and void sort(char *states[]) Commented Dec 21, 2015 at 19:26
  • Ok, I see. So I was not accepting the array of pointers. Commented Dec 21, 2015 at 19:28

5 Answers 5

13

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);
Sign up to request clarification or add additional context in comments.

4 Comments

sort function must accept a pointer to pointer. Ultimately, char *states[] is equivalent to char **states as a function parameter, therefore char *states[] is not an array of pointers.
@haccks: You are correct. The argument from the caller was the name of an array, and the sort function must have a prototype that allows that argument to be passed. An array name decays to a pointer value equal to the address of its first element, so sort needs to accept a pointer to a char *.
So the char array pointer in main is always just initially pointing to the first element in that array, correct?
@user3495404; No char array pointer in main. states is an array of pointers to char. In function call sort(states, 4);, states converted to char ** (pointer to first element of array states, i.e pointer to state[0]).
3

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

2 Comments

Can you explain what the sizeof(states) / sizeof(char *) is doing?
states is an array of pointer to char. char * is a pointer to char. So that division specifies the number of elements (ie, the number of char pointers) in the states array of pointers.
1

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

    }

Comments

1

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

Comments

0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.