Structure usage issue

Discussion in 'C Programming' started by Pedro Pinto, Dec 5, 2006.

  1. Pedro Pinto

    Pedro Pinto Guest

    Hi there!

    I'm presented with the following situation:

    I'm writing a server program that receives information and saves it to
    a structure i've created that goes by the name of tabela.

    The entire program has 6 files:

    projecto.h
    serv.h
    servsql.h
    linkedlist.c
    serv_aux.c
    servsql.c // this is were main() is

    My issue is, i've declared the structure tabela in the file linkedlist.
    I can use the functions defined in linkedlist in servsql (were main
    is), but i cannot create a variable of the type tabela there... What is
    wrong??? I've searched the web for some tutorial but failed to find any
    result. Thanks in advance for any help. Bellow you can see the
    beginning of every file.

    Regards

    Pedro Pinto


    ------------------------------
    projecto.h
    ------------------------------
    #ifndef PROJECTO_H
    #define PROJECTO_H


    #define BUFFSIZE 6804
    #define LENGTH 4


    #include <sys/socket.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <errno.h>
    #include <sys/time.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>

    #endif

    ------------------------------
    servsql.h
    ------------------------------
    #ifndef SERVSQL
    #define SERVSQL

    #include "linkedlist.c"
    #include "serv_aux.c"
    #include "servsql.c"

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

    void syntax();

    void checkParameters(int argc);

    void checkPort(int porto_servidor);



    #endif

    -----------------------------
    serv.h
    -----------------------------

    #ifndef SERV_H
    #define SERV_H


    #include "projecto.h"

    void syntax();

    void checkParameters(int argc);

    void checkPort(int porto_servidor);

    void printMessage(char buff[]);

    void trataMsgCreateInsert(char array[], char result[]);

    void trataMsgUpdateSelect(char array[], char result[]);

    int criaMensagem(int id, int cod, char pedido[], char *buffer);

    void getPedido(char buf[], char result[]);

    int getId(char buf[]);

    int getCod(char buf[]);

    int getSizeQuery(char buf[]);


    ------------------------------------
    servsql.c
    ------------------------------------

    #include "serv.h"


    /* Função Main */
    int main (int argc, char *argv[]) {

    ------------------------------------
    serv_aux.c
    ------------------------------------

    #include "serv.h"


    ------------------------------------
    linkedlist.c
    ------------------------------------

    #include "projecto.h"



    typedef struct tabela tabela;
    struct tabela {
    char nome[50];
    int nColunas;
    int nLinhas;
    char nomeColunas[10][50];
    char valores[50][10][65];
    tabela *next;
    };
     
    Pedro Pinto, Dec 5, 2006
    #1
    1. Advertising

  2. Pedro Pinto

    santosh Guest

    Pedro Pinto wrote:
    > Hi there!
    >
    > I'm presented with the following situation:
    >
    > I'm writing a server program that receives information and saves it to
    > a structure i've created that goes by the name of tabela.
    >
    > The entire program has 6 files:
    >
    > projecto.h
    > serv.h
    > servsql.h
    > linkedlist.c
    > serv_aux.c
    > servsql.c // this is were main() is
    >
    > My issue is, i've declared the structure tabela in the file linkedlist.
    > I can use the functions defined in linkedlist in servsql (were main
    > is), but i cannot create a variable of the type tabela there... What is
    > wrong??? I've searched the web for some tutorial but failed to find any
    > result. Thanks in advance for any help. Bellow you can see the
    > beginning of every file.


    Put common declarations like the tabela into one of the header files
    and include it from servsql.c. If you want to use an opaque structure
    then you must provide accessor functions.
     
    santosh, Dec 5, 2006
    #2
    1. Advertising

  3. Pedro Pinto

    CBFalconer Guest

    Pedro Pinto wrote:
    >
    > I'm writing a server program that receives information and saves
    > it to a structure i've created that goes by the name of tabela.
    >
    > The entire program has 6 files:
    >
    > projecto.h
    > serv.h
    > servsql.h
    > linkedlist.c
    > serv_aux.c
    > servsql.c // this is were main() is
    >
    > My issue is, i've declared the structure tabela in the file
    > linkedlist. I can use the functions defined in linkedlist in
    > servsql (were main is), but i cannot create a variable of the
    > type tabela there... What is wrong??? I've searched the web for
    > some tutorial but failed to find any result. Thanks in advance
    > for any help. Bellow you can see the beginning of every file.


    Most of your post is off-topic, because it refers to things unknown
    to standard C. To start with, all the following headers are
    unknown:

    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <netdb.h>

    but there is a larger C question here. If you defined your
    "tabela" in linkedlist.c, and need that definition elsewhere, you
    need to publish it in the interface definition for linkedlist.c.
    The usual way to do this is to place the definition in
    linkedlist.h, and #include "linkedlist.h" in every .c file that
    needs to know about it.

    The purpose of .h files is to expose the portions of the
    corresponding .c file that you wish known elsewhere. You also
    #include "linkedlist.h" in linkedlist.c to ensure that it also uses
    the same definitions or prototypes. You then #include
    "linkedlist.h" in each .c file that needs to access those
    definitions or prototypes.

    Think of the .h file as the link between independent .c files. Not
    as a convenient place to stuff prototypes etc. You *never* declare
    variables or function bodies in a .h file. Also get in the habit
    of marking all functions and file scope variables (in the .c file)
    which you do not wish to expose as static.

    Before posting again here, read the links below:

    --
    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
    <http://www.dinkumware.com/refxc.html> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net> (C-info)
     
    CBFalconer, Dec 5, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,889
    Robert Olofsson
    Oct 21, 2003
  2. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    575
    Colin J. Williams
    Feb 26, 2004
  3. hvt
    Replies:
    0
    Views:
    1,241
  4. hvt
    Replies:
    0
    Views:
    1,528
  5. Krist
    Replies:
    8
    Views:
    6,891
    Arne Vajhøj
    Feb 10, 2010
Loading...

Share This Page