Linked list with bidimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro Pinto

    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.

  • Chris Dollin

    #2
    Re: Linked list with bidimensional array

    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

    Comment

    • John Gordon

      #3
      Re: Linked list with bidimensional array

      In <1162309573.571 246.293290@h48g 2000cwc.googleg roups.com"Pedro Pinto" <kubic62@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."
      gordon@panix.co m

      Comment

      • Adrian

        #4
        Re: Linked list with bidimensional array

        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

        Comment

        • mark_bluemel@pobox.com

          #5
          Re: Linked list with bidimensional array


          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(tab ela *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(t abela));
          /* 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;thi s != NULL; this=this->next) {
          printTabela(thi s);
          }

          }

          Comment

          • Pedro Pinto

            #6
            Re: Linked list with bidimensional array

            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(tabe la *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.nome Colunas[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:

            Comment

            • Pedro Pinto

              #7
              Re: Linked list with bidimensional array

              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(tabe la *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.nome Colunas[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.

              Comment

              • Pedro Pinto

                #8
                Re: Linked list with bidimensional array

                i've tried the solution:

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

                and it doesn't work.

                But if i write this instead:

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

                IT WORKS!!! =)

                Thanks for the help!

                Comment

                • John Gordon

                  #9
                  Re: Linked list with bidimensional array

                  In <1162312854.907 411.42010@b28g2 000cwb.googlegr oups.com"Pedro Pinto" <kubic62@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.nome Colunas[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."
                  gordon@panix.co m

                  Comment

                  • John Gordon

                    #10
                    Re: Linked list with bidimensional array

                    In <1162313519.533 129.160490@m7g2 000cwm.googlegr oups.com"Pedro Pinto" <kubic62@gmail. comwrites:
                    i've tried the solution:
                    strcpy(tmp.valo res[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."
                    gordon@panix.co m

                    Comment

                    • Default User

                      #11
                      Re: Linked list with bidimensional array

                      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

                      Comment

                      • Pedro Pinto

                        #12
                        Re: Linked list with bidimensional array

                        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

                        Comment

                        Working...