Thread: Runtime Check #3 variable used not defined - Structs pointer error

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Runtime Check #3 variable used not defined - Structs pointer error

    Hi,
    I am fairly new to programming using structs in C and I get the following when trying to assign a value to a member variable of type struct. Any inputs on getting this addressed will be much appreciated.

    Here is the code for the program I am using:
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	struct employee {
                    char name[30];
                    int emp_id;
                    float salary;
            };
    		struct employee *emp2;
            emp2->emp_id = 3456; // error occurs here
            return 0;
    
    }
    The specific error message is Run-TIme Check Failure #3 - The variable emp2 is being used without being defined.

    Thanks,
    Manisha
    Last edited by Salem; 02-27-2010 at 04:02 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct employee *emp2;
    emp2->emp_id = 3456; // error occurs here
    Where does emp2 point to? As a pointer it must point to a valid memory location. Currently it is an uninitialized pointer with some random value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    structure variable

    You created the structure variable as a pointer. As a pointer it must point to a valid memory location
    You didn't assign any address to that pointer variable. So that you got an error.

    Try this,

    Code:
    struct employee *emp2;
    emp2=(struct employee *) malloc(sizeof(struct employee));
    emp2->emp_id = 3456;           // problem solved

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need cast malloc in C - see FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by abinila View Post
    You created the structure variable as a pointer. As a pointer it must point to a valid memory location
    You didn't assign any address to that pointer variable. So that you got an error.

    Try this,

    Code:
    struct employee *emp2;
    emp2=(struct employee *) malloc(sizeof(struct employee));
    emp2->emp_id = 3456;           // problem solved
    I would urge you, and the OP, to read the links posted, because it covers exactly why it doesn't work and what you just typed out in vain now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM