Multi dimension array... turn brain into mash potato!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am trying to produce multi dimension array based on address list from Customer table (from MYSQL) to be printed on label(s). I am failing to create 6 rows, by 3 column array OR any given dimension. Following is source code which I am trying to work with my requirement ( Or predicament) but not single iota of success.
If any one has any clue please, greatly appreciated, Thanks in advance.
/*
Multidimension array of Address data, extracted from table.
The intended target result should look as follow:
[1] Mr Joe Blogg [2] Mrs Joe Blogg [3] Mstr Joe Blogg Jr.
10 My street, 10 My Street, 10 My Street,
My home town, My Home town, My Home town,
POST CODE POST CODE POST CODE
[4] Mr.O Smith [5] Mrs O Smith [6] Mstr A Smith Jr.
11 White Gdns 11 White Gdns 11 White Gdns
Garden's Lane, Garden's Lane, Garden's Lane,
Home County Home County Home County
POST CODE POST CODE POST CODE
gchar *psqlqry
;
psqlqry = g_strdup_printf("Select rtrim(Contact),rtrim(Company),rtrim(HouseFlatNo),rtrim(Street),rtrim(Area),rtrim(Town),rtrim(PostCod e) FROM Customer ORDER BY ActId");
conn_ptr = mysql_init(NULL); /* Initialise */
g_print("\x1B[2J"); /*Clear the screen */
/* Connect to database */
if (!mysql_real_connect(conn_ptr, server,user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn_ptr));
return(0);
}
/* Set all pointers to NULL this will make it possible to free() */
memset(array, 0, trec * sizeof(char*));
/* work for number of columns */
for(i=0; i < trec; i++) {
array[i] = (gchar*) malloc (Y * sizeof(gchar));
if(!array[i]) {
g_print("Not enough memory!\n");
cleanup(array, trec);
g_atexit(GINT_TO_POINTER(1));
}
}
/* Fill the array with data */
for (i=0; i < trec; i++){
row = mysql_fetch_row(res);
for(j=0; j < Y; j++)
array[i][j] = row[j];
}
display_array(array, trec, Y);
cleanup(array, trec);
#endif
/* Release memory used to store results and close connection */
if (mysql_eof(res))
mysql_free_result(res);
mysql_close(conn_ptr);
return 0;
}
int
main (int argc, char * argv[])
{
gtk_init (&argc, &argv);
First, use "code" tags when you post code, especially a giant piece of code like what you have. Its very difficult (or at least frustrating) to read and understand the code without it being formatted (i.e. using "code" tags).
Quote:
I am failing to create 6 rows, by 3 column array OR any given dimension.
So is the problem that you cant declare a 2-d array? If thats the case, then please tell us why you cant. That is, are there compile errors? Runtime errors? Or you just arent sure how? Please let us know which case it is, and if there are compiler errors, please highlight the lines of code the error is on, or just post those lines as well as the error message. You have a lot of code so its difficult to read it, understand it, reproduce the error, debug it, fix it. Its easier if you give us the starting point.
EDIT: Basically, make sure your code is doing something like the following. If it isnt, then we can start with what your doing differently:
Code:
int ROWS = 6;
int COLS = 3;
int** table;
table = malloc(sizeof(int*) * ROWS);
// make sure table isnt null, i.e. malloc didnt fail
int i;
for (i = 0; i< ROWS; i++)
{
table[i] = malloc(sizeof(int) * COLS);
// make sure malloc didnt fail
}
// ...
for (i = 0; i< ROWS; i++)
{
free( table[i]);
}
free(table);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.