On 09/11/2015 10:35 AM, Edward Bartolo wrote:
> Hi all,
>
> Since yesterday I have been trying to understand why "char**
> essid_list" is working inside getInstalledEssidList(&count,
> essid_list) but failing as soon as I try to access essid_list[0]
> outside the function.
>
> Both the source and the gdb text output are attached.
>
> Any helpful pointers are appreciated.
Having dynamically growing (and shrinking) lists in C is
always a problem. That's why the problem has been solved
many times. :-)
For example, the singly-linked list, GSList, from the GLIB
library is pretty popular and could save you some time.
I have attached an example program to demonstrate how GSList
can be used to manage a list of allocated C-type strings.
Best regards,
T.
/* An example program for GSList.
*
* Compile with:
*
* gcc $(pkg-config --libs glib-2.0 --cflags glib-2.0 ) -o example example.c
*
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
char * teststring[] = { "hello", "world", NULL };
int main(int argc, char** argv) {
GSList * list = NULL;
// Fill the example list:
char ** a_string = teststring;
while(*a_string) {
list = g_slist_append(list, strdup(*a_string));
a_string++;
}
// Iterate the filled list:
int i = 0, l = g_slist_length(list);
for(i=0; i<l; i++)
printf("item #%d is \"%s\"\n", i, g_slist_nth_data(list, i));
// Free the list and the data strings inside (using free()):
g_slist_free_full(list, free);
return 0;
}