Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 11-05-2009, 06:26 PM   #1
LQ Newbie
Registered: Oct 2009
Posts: 25

Rep: Reputation: 0
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:

# include <config.h>

#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,

[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

Makefile script
CC = gcc

CFLAGS = -Wall \

rnm: rnm.c
$(CC) rnm.c -o rnm $(DEPRECTATED) `pkg-config libgnomeui-2.0 --cflags --libs`

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++;

void cleanup(gchar** array, gint x){
gint i;

for(i=0; i < x; i++)


int check_addresses()
MYSQL *conn_ptr;

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));

/* send SQL query */
if (mysql_query(conn_ptr, psqlqry)) {
fprintf(stderr, "%s\n", mysql_error(conn_ptr));

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++)

int i, j;

gchar** array = (char**)malloc(trec * sizeof(char*));

if(!array) {
g_print("Not enough memory!\n");

/* 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);
/* 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);

/* Release memory used to store results and close connection */
if (mysql_eof(res))


return 0;

main (int argc, char * argv[])
gtk_init (&argc, &argv);


g_print ("Done...\n");

return 0;

Thanks ....

Old 11-05-2009, 09:38 PM   #2
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
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).

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:
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]);


Last edited by nadroj; 11-05-2009 at 09:41 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Many dimension array ratsietsi Programming 3 09-17-2006 02:28 PM
Multi-dimension array problem. ArthurHuang Programming 2 05-20-2006 10:36 AM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 08:04 PM
Multi-line return from grep into an array? rose_bud4201 Programming 2 06-14-2005 05:11 PM
Working with array of multi-lun troyzeng Linux - General 4 11-15-2003 05:48 AM

All times are GMT -5. The time now is 04:50 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration