LQ Newbie
Registered: Oct 2009
Posts: 25
Rep:
|
Multi dimension array... turn brain into mash potato!
Hi Guys,
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.
The code as follow:
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gnome.h>
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#define Y 3
/*
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
Makefile script
CC = gcc
CFLAGS = -Wall \
-DG_DISABLE_DEPRECATED \
-DGDK_DISABLE_DEPRECATED \
-DGDK_PIXBUF_DISABLE_DEPRECATED \
-DGTK_DISABLE_DEPRECATED
rnm: rnm.c
$(CC) rnm.c -o rnm $(DEPRECTATED) `pkg-config libgnomeui-2.0 --cflags --libs`
clean:
rm -f *.o rnm
*/
void display_array(gchar** array, gint num_row, gint num_cols)
{
gint i, j, k;
k = 1;
for(i=0; i < num_row; i++) {
for(j=0; j < num_cols; j++)
g_print("[%02d] %d ", k, array[i][j]); k++;
g_print("\n");
}
}
void cleanup(gchar** array, gint x){
gint i;
for(i=0; i < x; i++)
free(array[i]);
free(array);
}
static
int check_addresses()
{
MYSQL *conn_ptr;
MYSQL_RES *res;
MYSQL_ROW row;
gchar *server = "localhost";
gchar *user = "newsagent";
gchar *password = "newsagent";
gchar *database = "chouse";
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);
}
/* send SQL query */
if (mysql_query(conn_ptr, psqlqry)) {
fprintf(stderr, "%s\n", mysql_error(conn_ptr));
return(0);
}
g_print("%s, (Query Length: %ul)\n", psqlqry, strlen(psqlqry));
res = mysql_store_result(conn_ptr);
gint trec = mysql_num_rows(res); /* extract total number of recs */
g_print("Total records: %i\n", trec);
#ifdef __FOR_LOOP__
while((row = mysql_fetch_row(res)))
{
gint i = 0;
for (i=0 ; i < mysql_num_fields(res); i++)
printf("\n%s",row[i]);
fputc('\n',stdout);
}
#else
int i, j;
gchar** array = (char**)malloc(trec * sizeof(char*));
if(!array) {
g_print("Not enough memory!\n");
g_atexit(GINT_TO_POINTER(1));
}
/* 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);
check_addresses();
g_print ("Done...\n");
return 0;
}
Thanks ....
Sh
|