int *setting_value_row; int *setting_value; char *field_name; char *tmp_setting_name; int nFields; int i; int j; int compare; nFields = PQnfields(res); //find the setting name and the row associated to it for (i = 0; i < nFields; i++) { field_name = PQfname(res, i); compare = strcmp(field_name, "setting_name"); if(compare == 0) { printf("\n\nFound Setting name: %s\n\n", field_name); compare = 1; for (j = 0; j < nFields; j++) { tmp_setting_name = PQgetvalue(res, j, i); printf("\ntmp_setting_name is: %s", tmp_setting_name); compare = strcmp(tmp_setting_name, settingname); printf("\n looking for next setting, current is: %s , looking for: %s, compare is: %i", tmp_setting_name, settingname, compare); if(compare == 0) { printf("Found setting: %s", settingname); compare = 0; // We found which row our setting value is, end the loop. fprintf("Setting name found: '%s'\n", tmp_setting_name); setting_value_row = j; j=nFields; i=nFields; } } } } the output in gdb is: Found Setting name: setting_name tmp_setting_name is: monitor_desktop looking for next setting, current is: monitor_desktop , looking for: screenshot_interval, compare is: 1 tmp_setting_name is: storage_path looking for next setting, current is: storage_path , looking for: screenshot_interval, compare is: 1 tmp_setting_name is: screenshot_interval Program received signal SIGSEGV, Segmentation fault. 0xb7dfed8e in cuserid () from /lib/libc.so.6