Thread: Daft Question

  1. #1
    Registered User
    Join Date
    Nov 2011

    Daft Question

    Hi There,

    Is there anything obvious as to why this wouldn't work, basically i have three files. Two .c files and one .h

    in the .h i have a structure called "structure"

    below it:

    struct structure example[1];

    in the two .c files i have included the .h file but for some reason the variables are not carrying across between the two functions. For example in function 1

    example[1].IpIndex = 1;

    but in the following function 2

    example[1].IpIndex is showing as 0.

    Bit difficult to try and explain but any ideas as to if this should work and is there something else interfering or am i totally barking up wrong tree.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Well how about you post your actual code and the errors you are getting and that should make it pretty crystal clear. Also, what is the point of declaring an array of size 1? And even if that had a point, why are you trying to access array[1] which doesn't exist, since counting starts at 0?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Join Date
    Dec 2007
    struct structure example[1];
    A 1 element array, while ridiculous on its face, has one valid index: 0. What index are you using here?

    example[1].IpIndex = 1;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The .h file (in addition to the structure typedef) should have

    extern struct structure example[1];
    Exactly ONE .c file should have
    struct structure example[1];
    Without the extern in the .h file, you just end up with "multiply declared" error messages from the linker.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Daft Question Time
    By r_james14 in forum C Programming
    Replies: 6
    Last Post: 11-18-2011, 10:02 AM
  2. Everyone who reads this will laugh, such a daft question.
    By r_james14 in forum C Programming
    Replies: 8
    Last Post: 11-07-2011, 07:22 AM
  3. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  4. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM