Thread: How to get Extern struct to work properly ?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    14

    How to get Extern struct to work properly ?

    Apparently I failed to Call my struct. I have header file (header.h) where i defined my struct .
    Header.h
    Code:
    struct theVar {
    float x1, y1;
    };
    extern struct theVar global;
    
    
    ...
    
    
    struct theVar compute (float x, float y){
      float x1 = x + y;
      float y1 = x * y;
    
    
      struct theVar result;
      result.x1;
      result.y1;
      return result;
    }
    and then in my C file, I call them :
    file1.c
    Code:
    #include <header.h>
    struct theVar global;
    
    
    int main (){
     
    struct theVar global = compute(3,4);
    
    
    printf("x1 = %f \n", global.x1 );
    printf("y1 = %f \n", global.y1);
    }
    But my program failed to call the value. What did I miss ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It doesn't look like you need a global struct in the first place: you have a local struct named global to store the return value from compute, and this should work, although it also means that your global struct named global remains unused. If you really want to use the global struct instead, then just write:
    Code:
    global = compute(3, 4);
    But again it looks like you don't need a global struct. You should avoid global variables. (Actually, since you didn't spot the declaration of a local variable shadowing the global variable, I daresay you must not use your own global variables as you presently lack sufficient skill in C to use them well and appropriately.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    Quote Originally Posted by laserlight View Post
    It doesn't look like you need a global struct in the first place: you have a local struct named global to store the return value from compute, and this should work, although it also means that your global struct named global remains unused. If you really want to use the global struct instead, then just write:
    Code:
    global = compute(3, 4);
    But again it looks like you don't need a global struct. You should avoid global variables. (Actually, since you didn't spot the declaration of a local variable shadowing the global variable, I daresay you must not use your own global variables as you presently lack sufficient skill in C to use them well and appropriately.)
    hey , you reply to me again. thanks a lot for the suggestion ! Also, again for a few times now.. what a kind supportive word. I'm doing my best at school. wish me luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct and extern
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:39 AM
  2. cannot compile with extern struct
    By jeanluca in forum C Programming
    Replies: 4
    Last Post: 06-08-2009, 02:51 AM
  3. extern struct
    By beginner.c in forum C Programming
    Replies: 10
    Last Post: 07-01-2007, 07:43 PM
  4. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM
  5. extern struct...
    By Supar in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2003, 02:58 PM

Tags for this Thread