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.
}
}
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().
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.
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, 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 *));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With