473,412 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Linked list with bidimensional array

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.

Oct 31 '06 #1
11 2437
Pedro Pinto wrote:
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.

--
Chris "unhashedup hashed up hashing" Dollin
"The path to the web becomes deeper and wider" - October Project

Oct 31 '06 #2
In <11**********************@h48g2000cwc.googlegroups .com"Pedro Pinto" <ku*****@gmail.comwrites:
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.

--
John Gordon "It's certainly uncontaminated by cheese."
go****@panix.com

Oct 31 '06 #3
Pedro Pinto wrote:
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;
}

--

Adrian
Oct 31 '06 #4

Pedro Pinto wrote:
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[i]);
}
for (i = 0 ; i < this->nLinhas ; i++) {
printf("\t%s\n",this->valores[i]);
}
}

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

}

Oct 31 '06 #5
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:

Oct 31 '06 #6
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.

Oct 31 '06 #7
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!

Oct 31 '06 #8
In <11*********************@b28g2000cwb.googlegroups. com"Pedro Pinto" <ku*****@gmail.comwrites:
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...

--
John Gordon "It's certainly uncontaminated by cheese."
go****@panix.com

Oct 31 '06 #9
In <11**********************@m7g2000cwm.googlegroups. com"Pedro Pinto" <ku*****@gmail.comwrites:
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!!! =)
Members of structs are accessed differently if you have a struct
as opposed to a pointer-to-struct.

Structs use "."
Pointers-to-structs use "->"
Thanks for the help!
You're welcome.

--
John Gordon "It's certainly uncontaminated by cheese."
go****@panix.com

Oct 31 '06 #10
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
Oct 31 '06 #11
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

Oct 31 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
4
by: Anna | last post by:
Hi, I have the following MySQL table: inner_id data1 data2 data3 ->0 g sd ds 1 a n ...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
8
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project...
4
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.