Daft Question

This is a discussion on Daft Question within the C Programming forums, part of the General Programming Boards category; Hi There, Is there anything obvious as to why this wouldn't work, basically i have three files. Two .c files ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    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
    Location
    London, United Kingdom
    Posts
    2,094
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Code:
    struct structure example[1];
    A 1 element array, while ridiculous on its face, has one valid index: 0. What index are you using here?

    Code:
    example[1].IpIndex = 1;

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    The .h file (in addition to the structure typedef) should have

    Code:
    extern struct structure example[1];
    Exactly ONE .c file should have
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09: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, 06: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, 01:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21