Reading database in C and populating array of structure
I am reading from MySQl in C. But after populating structure in C when I print the values these are not printed.
MySQL Table:
mysql> select * from student;
+------+----------+-----------+----------------+-----------+----------------+
| id | name | subject_1 | subject_1_mark | subject_2 | subject_2_mark |
+------+----------+-----------+----------------+-----------+----------------+
| 1 | rock | phy | 45 | cse | 76 |
| 2 | scoot | phy | 76 | cse | 98 |
| 3 | ryan | math | 69 | ece | 99 |
| 4 | ronit | math | 56 | ece | 97 |
| 5 | raunak | che | 65 | mech | 99 |
| 6 | abhinav | che | 69 | mech | 79 |
| 7 | abhishek | mth | 97 | cse | 79 |
| 8 | montu | mth | 79 | cse | 98 |
| 9 | moti | mth | 99 | mech | 98 |
| 10 | jerry | mth | 94 | ml | 98 |
+------+----------+-----------+----------------+-----------+----------------+
MY code:
#include <mysql/mysql.h>
#include <stdio.h>
#include<malloc.h>
/* gcc database.c -L/usr/lib/mysql -lmysqlclient
*
*/
struct student {
char *id;
char *name;
char *subject_1;
char *subject_mark_1;
char *subject_2;
char *subject_mark_2;
struct student *next;
};
struct studentDB {
struct student *next;
};
void main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "my_sql_password"; /* set me first */
char *database = "new_db";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "select * from student")) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output rows from table */
struct studentDB *db = (struct studentDB *)malloc(sizeof(struct studentDB));
db->next = NULL;
printf("MySQL Tables in mysql database:n");
printf("IdtNametsubject_1tsubject_mark_1tsubject_2tsubject_mark_2n");
while ((row = mysql_fetch_row(res)) != NULL){
struct student *record = (struct student *)malloc(sizeof(struct student));
record->id = row[0];
//printf("record->id %sn",record->id);
record->name = row[1];
//printf("record->id %sn",record->name);
record->subject_1 = row[2];
//printf("record->id %sn",record->subject_1);
record->subject_mark_1 = row[3];
//printf("record->id %sn",record->subject_mark_1);
record->subject_2 = row[4];
//printf("record->id %sn",record->subject_2);
record->subject_mark_2 = row[5];
//printf("record->id %sn",record->subject_mark_2);
record->next = db->next;
//printf("current record %pn",record);
db->next = record;
//printf("next record id %pn",db->next->next);
//printf("%s %s %s %s %s %sn",row[0],row[1],row[2],row[3],row[4],row[5]);
}
struct student *ptr = db->next;
//printf("record->id %pn",db->next);
while(ptr!=NULL) {
printf("nrecord id %s",ptr->name);
printf("n");
ptr = ptr->next;
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
My output is:
record id
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
mysql c database libmysql
add a comment |
I am reading from MySQl in C. But after populating structure in C when I print the values these are not printed.
MySQL Table:
mysql> select * from student;
+------+----------+-----------+----------------+-----------+----------------+
| id | name | subject_1 | subject_1_mark | subject_2 | subject_2_mark |
+------+----------+-----------+----------------+-----------+----------------+
| 1 | rock | phy | 45 | cse | 76 |
| 2 | scoot | phy | 76 | cse | 98 |
| 3 | ryan | math | 69 | ece | 99 |
| 4 | ronit | math | 56 | ece | 97 |
| 5 | raunak | che | 65 | mech | 99 |
| 6 | abhinav | che | 69 | mech | 79 |
| 7 | abhishek | mth | 97 | cse | 79 |
| 8 | montu | mth | 79 | cse | 98 |
| 9 | moti | mth | 99 | mech | 98 |
| 10 | jerry | mth | 94 | ml | 98 |
+------+----------+-----------+----------------+-----------+----------------+
MY code:
#include <mysql/mysql.h>
#include <stdio.h>
#include<malloc.h>
/* gcc database.c -L/usr/lib/mysql -lmysqlclient
*
*/
struct student {
char *id;
char *name;
char *subject_1;
char *subject_mark_1;
char *subject_2;
char *subject_mark_2;
struct student *next;
};
struct studentDB {
struct student *next;
};
void main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "my_sql_password"; /* set me first */
char *database = "new_db";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "select * from student")) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output rows from table */
struct studentDB *db = (struct studentDB *)malloc(sizeof(struct studentDB));
db->next = NULL;
printf("MySQL Tables in mysql database:n");
printf("IdtNametsubject_1tsubject_mark_1tsubject_2tsubject_mark_2n");
while ((row = mysql_fetch_row(res)) != NULL){
struct student *record = (struct student *)malloc(sizeof(struct student));
record->id = row[0];
//printf("record->id %sn",record->id);
record->name = row[1];
//printf("record->id %sn",record->name);
record->subject_1 = row[2];
//printf("record->id %sn",record->subject_1);
record->subject_mark_1 = row[3];
//printf("record->id %sn",record->subject_mark_1);
record->subject_2 = row[4];
//printf("record->id %sn",record->subject_2);
record->subject_mark_2 = row[5];
//printf("record->id %sn",record->subject_mark_2);
record->next = db->next;
//printf("current record %pn",record);
db->next = record;
//printf("next record id %pn",db->next->next);
//printf("%s %s %s %s %s %sn",row[0],row[1],row[2],row[3],row[4],row[5]);
}
struct student *ptr = db->next;
//printf("record->id %pn",db->next);
while(ptr!=NULL) {
printf("nrecord id %s",ptr->name);
printf("n");
ptr = ptr->next;
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
My output is:
record id
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
mysql c database libmysql
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07
add a comment |
I am reading from MySQl in C. But after populating structure in C when I print the values these are not printed.
MySQL Table:
mysql> select * from student;
+------+----------+-----------+----------------+-----------+----------------+
| id | name | subject_1 | subject_1_mark | subject_2 | subject_2_mark |
+------+----------+-----------+----------------+-----------+----------------+
| 1 | rock | phy | 45 | cse | 76 |
| 2 | scoot | phy | 76 | cse | 98 |
| 3 | ryan | math | 69 | ece | 99 |
| 4 | ronit | math | 56 | ece | 97 |
| 5 | raunak | che | 65 | mech | 99 |
| 6 | abhinav | che | 69 | mech | 79 |
| 7 | abhishek | mth | 97 | cse | 79 |
| 8 | montu | mth | 79 | cse | 98 |
| 9 | moti | mth | 99 | mech | 98 |
| 10 | jerry | mth | 94 | ml | 98 |
+------+----------+-----------+----------------+-----------+----------------+
MY code:
#include <mysql/mysql.h>
#include <stdio.h>
#include<malloc.h>
/* gcc database.c -L/usr/lib/mysql -lmysqlclient
*
*/
struct student {
char *id;
char *name;
char *subject_1;
char *subject_mark_1;
char *subject_2;
char *subject_mark_2;
struct student *next;
};
struct studentDB {
struct student *next;
};
void main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "my_sql_password"; /* set me first */
char *database = "new_db";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "select * from student")) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output rows from table */
struct studentDB *db = (struct studentDB *)malloc(sizeof(struct studentDB));
db->next = NULL;
printf("MySQL Tables in mysql database:n");
printf("IdtNametsubject_1tsubject_mark_1tsubject_2tsubject_mark_2n");
while ((row = mysql_fetch_row(res)) != NULL){
struct student *record = (struct student *)malloc(sizeof(struct student));
record->id = row[0];
//printf("record->id %sn",record->id);
record->name = row[1];
//printf("record->id %sn",record->name);
record->subject_1 = row[2];
//printf("record->id %sn",record->subject_1);
record->subject_mark_1 = row[3];
//printf("record->id %sn",record->subject_mark_1);
record->subject_2 = row[4];
//printf("record->id %sn",record->subject_2);
record->subject_mark_2 = row[5];
//printf("record->id %sn",record->subject_mark_2);
record->next = db->next;
//printf("current record %pn",record);
db->next = record;
//printf("next record id %pn",db->next->next);
//printf("%s %s %s %s %s %sn",row[0],row[1],row[2],row[3],row[4],row[5]);
}
struct student *ptr = db->next;
//printf("record->id %pn",db->next);
while(ptr!=NULL) {
printf("nrecord id %s",ptr->name);
printf("n");
ptr = ptr->next;
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
My output is:
record id
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
mysql c database libmysql
I am reading from MySQl in C. But after populating structure in C when I print the values these are not printed.
MySQL Table:
mysql> select * from student;
+------+----------+-----------+----------------+-----------+----------------+
| id | name | subject_1 | subject_1_mark | subject_2 | subject_2_mark |
+------+----------+-----------+----------------+-----------+----------------+
| 1 | rock | phy | 45 | cse | 76 |
| 2 | scoot | phy | 76 | cse | 98 |
| 3 | ryan | math | 69 | ece | 99 |
| 4 | ronit | math | 56 | ece | 97 |
| 5 | raunak | che | 65 | mech | 99 |
| 6 | abhinav | che | 69 | mech | 79 |
| 7 | abhishek | mth | 97 | cse | 79 |
| 8 | montu | mth | 79 | cse | 98 |
| 9 | moti | mth | 99 | mech | 98 |
| 10 | jerry | mth | 94 | ml | 98 |
+------+----------+-----------+----------------+-----------+----------------+
MY code:
#include <mysql/mysql.h>
#include <stdio.h>
#include<malloc.h>
/* gcc database.c -L/usr/lib/mysql -lmysqlclient
*
*/
struct student {
char *id;
char *name;
char *subject_1;
char *subject_mark_1;
char *subject_2;
char *subject_mark_2;
struct student *next;
};
struct studentDB {
struct student *next;
};
void main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "my_sql_password"; /* set me first */
char *database = "new_db";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "select * from student")) {
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output rows from table */
struct studentDB *db = (struct studentDB *)malloc(sizeof(struct studentDB));
db->next = NULL;
printf("MySQL Tables in mysql database:n");
printf("IdtNametsubject_1tsubject_mark_1tsubject_2tsubject_mark_2n");
while ((row = mysql_fetch_row(res)) != NULL){
struct student *record = (struct student *)malloc(sizeof(struct student));
record->id = row[0];
//printf("record->id %sn",record->id);
record->name = row[1];
//printf("record->id %sn",record->name);
record->subject_1 = row[2];
//printf("record->id %sn",record->subject_1);
record->subject_mark_1 = row[3];
//printf("record->id %sn",record->subject_mark_1);
record->subject_2 = row[4];
//printf("record->id %sn",record->subject_2);
record->subject_mark_2 = row[5];
//printf("record->id %sn",record->subject_mark_2);
record->next = db->next;
//printf("current record %pn",record);
db->next = record;
//printf("next record id %pn",db->next->next);
//printf("%s %s %s %s %s %sn",row[0],row[1],row[2],row[3],row[4],row[5]);
}
struct student *ptr = db->next;
//printf("record->id %pn",db->next);
while(ptr!=NULL) {
printf("nrecord id %s",ptr->name);
printf("n");
ptr = ptr->next;
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
My output is:
record id
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
record id "
mysql c database libmysql
mysql c database libmysql
edited Nov 22 '18 at 19:59
Govind Parmar
12.5k53563
12.5k53563
asked Nov 22 '18 at 19:32
LCPLCP
113
113
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07
add a comment |
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437132%2freading-database-in-c-and-populating-array-of-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437132%2freading-database-in-c-and-populating-array-of-structure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's not explicitly said in the documentation how mysql_use_result works with the memory. Does your code work with mysql_store_result? I am not sure if you can really relay on the fact that all memory stays allocated if you read one line at a time. Can you run a debugger and after you store second row in your db->next check how does db->next->next look like? Quite possibly you may need to copy the result to memory allocated by you. I used this library 20 years ago, so I'm sorry that I don't remember all details. :)
– petrch
Nov 22 '18 at 20:05
$6 = {id = 0x555555770fb1 "2", name = 0x555555770fb3 "scoot", subject_1 = 0x555555770fbc "76", subject_mark_1 = 0x555555770fc0 "se", subject_2 = 0x555555770fc3 "98", subject_mark_2 = 0x555555770fc7 "76", next = 0x0}
– LCP
Nov 23 '18 at 3:32
It works if I changed structure to use array:
– LCP
Nov 23 '18 at 3:51
struct student { int id; char name[20]; char subject_1[20]; float subject_mark_1; char subject_2[20]; float subject_mark_2; struct student *next; };
– LCP
Nov 23 '18 at 3:51
I am afraid that "when I used array it works" looks like a memory leak. You maybe do allocate array, but unless you copy the strings into those arrays it possibly works only because of some random memory leak. Internally char[20] is still char* and you just forgetting the memory for the array as you overwrite the pointer. My rule of thumb is unless the documentation says "the function returns an allocated string" or I see it in the source code I don't expect it to be true.
– petrch
Nov 23 '18 at 22:07