I've managed to point towards an array, however, the closest I've gotten to pointing to a char* list only prints out the individual letters as shown below:
#include <stdio.h>
main()
{
char* list[2];
list[0] = "Hullo";
list[1] = "GoodBye";
char* pointer = &(list);
char* back = *(list);
printf("%c", back[8]);
}
0 through 4 of back prints out "Hullo", 5 through 7 is whitespace, and 8 through 14 prints out "Goodbye".
I'm wondering how I can avoid printing this list character to character, as it becomes a very inconvenient issue when returning lists of unspecified sizes and planning on using them for another function etc.
It feels like you're looking for:
#include <stdio.h>
int
main(void)
{
char * list[2];
list[0] = "Hullo";
list[1] = "GoodBye";
char ** pointer = list;
for( size_t i = 0; i < sizeof list / sizeof *list; i++ ){
printf("%s\n", *pointer++);
}
}
but maybe you're looking for something like:
#include <stdio.h>
#include <stdlib.h>
char **
foo(void)
{
char **p = malloc(3 * sizeof *p);
if( p == NULL ){
perror("malloc");
exit(1);
}
p[0] = "Hullo";
p[1] = "GoodBye";
p[2] = NULL;
return p;
}
int
main(void)
{
char **p = foo();
for( ; *p; p++ ){
printf("%s\n", *p);
}
free(p);
}
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.