Pointers to elements in a Double-Linkedlist

Discussion in 'C Programming' started by Paminu, Feb 13, 2006.

  1. Paminu

    Paminu Guest

    I am implementing a double-linkedlist in C. I have defined some pointers
    that points to different locations in my list. Before using the list I will
    initialize these pointers and when I call insert/delete etc they will be
    changed.

    Instead of giving each funtion these pointers as argumentens I was wondering
    if it was possible to place them i a .h file and initialize them there. When
    a function changes the pointers they will be updated in the .h file which
    will always contain the most updated version of the pointers.

    Is this possible?

    Johs
     
    Paminu, Feb 13, 2006
    #1
    1. Advertising

  2. Paminu

    CBFalconer Guest

    Paminu wrote:
    >
    > I am implementing a double-linkedlist in C. I have defined some pointers
    > that points to different locations in my list. Before using the list I will
    > initialize these pointers and when I call insert/delete etc they will be
    > changed.
    >
    > Instead of giving each funtion these pointers as argumentens I was wondering
    > if it was possible to place them i a .h file and initialize them there. When
    > a function changes the pointers they will be updated in the .h file which
    > will always contain the most updated version of the pointers.
    >
    > Is this possible?


    Possible, but inadvisable. You should NEVER define data in a .h
    file. The purpose of the header file is to define the access to
    your module given to other modules. Thus is will necessarily be
    included in more than one compilation unit, and any data
    definitions will result in multi-defined errors.

    --
    "The power of the Executive to cast a man into prison without
    formulating any charge known to the law, and particularly to
    deny him the judgement of his peers, is in the highest degree
    odious and is the foundation of all totalitarian government
    whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
     
    CBFalconer, Feb 13, 2006
    #2
    1. Advertising

  3. On Mon, 13 Feb 2006 09:23:26 +0100, "Paminu" <> wrote:

    >I am implementing a double-linkedlist in C. I have defined some pointers
    >that points to different locations in my list. Before using the list I will
    >initialize these pointers and when I call insert/delete etc they will be
    >changed.
    >
    >Instead of giving each funtion these pointers as argumentens I was wondering
    >if it was possible to place them i a .h file and initialize them there. When
    >a function changes the pointers they will be updated in the .h file which
    >will always contain the most updated version of the pointers.
    >
    >Is this possible?


    Possible, yes. Desirable, probably not.

    ..h files, as the term is used in C, are used during compilation, not
    during linking and not during execution.

    Most of the experienced contributors recommend not placing object or
    function definitions in a .h file, only declarations, typedefs, macro
    definitions, etc. If your program has multiple source files that
    include the .h files and you put the pointer definitions and
    initialization in the .h file, then when you link the multiple object
    modules the linker will report multiple definitions of the pointers.

    If you want to establish the pointers as global variables so that all
    of your functions have access to them, the preferred approach is to
    declare them as EXTERN in the .h file, define them in the source file
    containing main(), and #include the .h file in every source module.
    However, the conventional wisdom is avoid globals unless absolutely
    necessary.


    Remove del for email
     
    Barry Schwarz, Feb 14, 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. Tohru Kao
    Replies:
    3
    Views:
    454
    Neil Masson
    Jul 14, 2003
  2. Tohru Kao
    Replies:
    1
    Views:
    414
    Chris
    Jul 8, 2003
  3. David
    Replies:
    4
    Views:
    767
    Jonathan Oexner
    Jul 31, 2003
  4. Sydex
    Replies:
    12
    Views:
    6,599
    Victor Bazarov
    Feb 17, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    710
Loading...

Share This Page