Linked list with bidimensional array

P

Pedro Pinto

Hi there! I'm having a problem in my program.

i've created this structure:

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};


my problem is the following:

I can sucessfully alter the values of nome[50], nColunas and nLinhas,
but i can't seem to be able to insert a string into nomeColunas or
valores.

This is to simulate an SQL table, nomeColunas[10][50] indicates that
you can have 10 diferent collum names with a 50 bytes name length.

Valores[50][65] indicates that you can have 50 lines of values with a
50 bytes name length.

My question is, how can i create a function that permits me to alter
the value on these two bidimensional arrays?

Thanks in advance for any help.
 
C

Chris Dollin

Pedro said:
Hi there! I'm having a problem in my program.

i've created this structure:

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};


my problem is the following:

I can sucessfully alter the values of nome[50], nColunas and nLinhas,
but i can't seem to be able to insert a string into nomeColunas or
valores.

Why not?

I mean, what do you do, and what happens?

I assume you've read anything in the FAQ that looked relevant,
and that you've got a decent C book to hand, and that you've
read its relevant sections, so you know what a C string is and
what C's "multi-dimensional arrays" really are.
 
J

John Gordon

In said:
typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};
I can sucessfully alter the values of nome[50], nColunas and nLinhas,
but i can't seem to be able to insert a string into nomeColunas or
valores.

You should be able to, for example:

strcpy(myTabela.nomeColunas[4], "Hello!");
strcpy(myTabela.valores[4], "Goodbye!");

Post your code, and we'll try to help you figure out what's wrong.
 
A

Adrian

Pedro said:
Hi there! I'm having a problem in my program.

i've created this structure:

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};


my problem is the following:

I can sucessfully alter the values of nome[50], nColunas and nLinhas,
but i can't seem to be able to insert a string into nomeColunas or
valores.

This is to simulate an SQL table, nomeColunas[10][50] indicates that
you can have 10 diferent collum names with a 50 bytes name length.

Valores[50][65] indicates that you can have 50 lines of values with a
50 bytes name length.
65 bytes in length
My question is, how can i create a function that permits me to alter
the value on these two bidimensional arrays?

Thanks in advance for any help.
How about this small example

#include <stdio.h>
#include <string.h>

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
struct tabela *next;
};

int main(int argc, char *argv[])
{
tabela x;
tabela *x_ptr;

x_ptr=&x;

strcpy(x_ptr->nome, "testname");
strcpy(x_ptr->nomeColunas[0], "testcol1");
strcpy(x_ptr->nomeColunas[1], "testcol2");

printf("%s\n", x.nome);
printf("%s\n", x.nomeColunas[0]);
printf("%s\n", x.nomeColunas[1]);

return 0;
}
 
M

mark_bluemel

Pedro said:
This is to simulate an SQL table, nomeColunas[10][50] indicates that
you can have 10 diferent collum names with a 50 bytes name length.

No - 10 different column names with upto 49 bytes name length if you
are going to use standard C string format (you need to keep one byte
for the terminating '\0' byte).
Valores[50][65] indicates that you can have 50 lines of values with a
50 bytes name length.

50 lines, with upto 64 bytes, based on what you've said.
My question is, how can i create a function that permits me to alter
the value on these two bidimensional arrays?

Like this perhaps?

#include <stdlib.h>
#include <stdio.h>

typedef struct tabela tabela;

struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};

void printTabela(tabela *this) {
int i;
printf("[%s]\n",this->nome);
for (i = 0 ; i < this->nColunas ; i++) {
printf("\t%s\n",this->nomeColunas);
}
for (i = 0 ; i < this->nLinhas ; i++) {
printf("\t%s\n",this->valores);
}
}

int main(void) {

tabela *first = NULL;
tabela *this;
int i;
int j;

for (i = 0; i < 3 ; i++) {
this = malloc(sizeof(tabela));
/* error checking omitted */
sprintf(this->nome,"tabela numero %d",i);
this->nColunas = i+1;
this->nLinhas = i+1;
for (j=0;j<i;j++) {
sprintf(this->nomeColunas[j],"tabela numero %d coluna
numero %d",i,j);
sprintf(this->valores[j],"tabela numero %d valore numero
%d",i,j);
}
this->next = first;
first = this;
}

for (this=first;this != NULL; this=this->next) {
printTabela(this);
}

}
 
P

Pedro Pinto

here is the code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};

char *ptr;

void *reset(tabela *list){
list->nColunas =0;
list->nLinhas =0;
return list;
}


tabela *addtolist(tabela *list, char *name, char *colunas) {
tabela *tmp, *cur;
tmp = (struct tabela *)malloc(sizeof(struct tabela));

reset(tmp);
ptr = &tmp->nomeColunas[0][0];
strcpy(tmp->nome, name);
int nc = list->nColunas;
strcpy(ptr, colunas);
tmp->nLinhas++;
tmp->next = NULL;


cur = list;
if (cur == NULL) {
list = tmp;
} else {
while (cur->next != NULL)
cur = cur->next;
cur->next = tmp;
}

return list;
}

main() { /* Main test program: examples of calling the above functions
*/

tabela *tmp;
tmp = (struct tabela *)malloc(sizeof(struct tabela));

strcpy(tmp->nome, "test"); // works well, prints well
-> strcpy(tmp.nomeColunas[4], "Hello!");
// error: type error in argument 1 to 'strcpy' found 'struct tabela'
expected 'pointer to char'
// left operand of . has incompatible type 'pointer to struct tabela'


I know that the addToList isn't functioning very well because i don't
know how i can insert values into nomeColunas[10][50] and
valores[50][65]

i just want to know how can i insert values into these bidimensional
arrays.

the compilation error is the following:
 
P

Pedro Pinto

here is the code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct tabela tabela;
struct tabela {
char nome[50];
int nColunas;
int nLinhas;
char nomeColunas[10][50];
char valores[50][65];
tabela *next;
};

char *ptr;

void *reset(tabela *list){
list->nColunas =0;
list->nLinhas =0;
return list;
}


tabela *addtolist(tabela *list, char *name, char *colunas) {
tabela *tmp, *cur;
tmp = (struct tabela *)malloc(sizeof(struct tabela));

reset(tmp);
ptr = &tmp->nomeColunas[0][0];
strcpy(tmp->nome, name);
int nc = list->nColunas;
strcpy(ptr, colunas);
tmp->nLinhas++;
tmp->next = NULL;


cur = list;
if (cur == NULL) {
list = tmp;
} else {
while (cur->next != NULL)
cur = cur->next;
cur->next = tmp;
}

return list;
}

main() { /* Main test program: examples of calling the above functions
*/

tabela *tmp;
tmp = (struct tabela *)malloc(sizeof(struct tabela));

strcpy(tmp->nome, "test"); // works well, prints well
-> strcpy(tmp.nomeColunas[4], "Hello!");
// error: type error in argument 1 to 'strcpy' found 'struct tabela'
expected 'pointer to char'
// left operand of . has incompatible type 'pointer to struct tabela'


I know that the addToList isn't functioning very well because i don't
know how i can insert values into nomeColunas[10][50] and
valores[50][65]

i just want to know how can i insert values into these bidimensional
arrays.
Thanks for your efford. I've researched several info on the web but i
only come with examples in one dimension char arrays.
 
P

Pedro Pinto

i've tried the solution:

strcpy(tmp.valores[4], "Goodbye!");

and it doesn't work.

But if i write this instead:

strcpy(tmp->valores[4], "Goodbye!");

IT WORKS!!! =)

Thanks for the help!
 
J

John Gordon

In said:
here is the code:
tabela *tmp;
tmp = (struct tabela *)malloc(sizeof(struct tabela));
strcpy(tmp->nome, "test"); // works well, prints well
-> strcpy(tmp.nomeColunas[4], "Hello!");
// error: type error in argument 1 to 'strcpy' found 'struct tabela'
expected 'pointer to char'
// left operand of . has incompatible type 'pointer to struct tabela'

Since you're using a pointer-to-struct you must use the -> operator
to access the member variables, instead of the "." operator, like so:

strcpy(tmp->nomeColunas[4], "Hello!");

It's odd that you made this error, though, since you correctly used
the -> operator in the line of code just above...
 
J

John Gordon

D

Default User

Pedro Pinto wrote:

I can sucessfully alter the values of nome[50], nColunas and nLinhas,
but i can't seem to be able to insert a string into nomeColunas or
valores.



Post a complete, minimal program that demonstrates the problem. We
can't fix code we can't see.




Brian
 
P

Pedro Pinto

Thank you for you're help, the problem is solved.

The issue was that i wasn't being able to insert strings into the
bidimensional arrays.

After the code presented by you the issue was solved.

Once again thank you for you're help.

Regards

Pedro Pinto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top