I have a function like char ** f()
. It returns the address of a variable char* a[]
.I stored this return type in variable char ** s
, but I want to print all the values of char * a[]
. How is this possible?
Thanx.
I have a function like char ** f()
. It returns the address of a variable char* a[]
.I stored this return type in variable char ** s
, but I want to print all the values of char * a[]
. How is this possible?
Thanx.
Just remember that you're dealing with an array of pointers.
Assuming (and this is a generally unsafe assumption) that your list of pointers ends with a null, in much the same way a string ends with a null, you can use a simple for loop to access the char* in the array similar to the following:
for (int index = 0; index++ ; a[index] != null) {
char * myString = a[index];
printf("Value: %s\n",myString);
}
The other posters are right, you need to know how many char *s you're dealing with, and so far that information isn't apparent. The above answer makes a HUGE assumption about the structure of the data you're dealing with, and WILL segfault your application if that assumption is incorrect.
sample
#include <stdio.h>
#include <stdlib.h>
char **f(void){
char **array = malloc(sizeof(char*)*6);
array[0] = "fox";
array[1] = "jumps";
array[2] = "over";
array[3] = "lazy";
array[4] = "dog";
array[5] = NULL;
return array;
}
int main() {
char ** s = f();
for(int i=0;s[i];++i)
printf("%s\n", s[i]);
free(s);
return 0;
}