Solved

Runtime Error: Warning about uninitialized variable

Posted on 2003-10-27
1
219 Views
Last Modified: 2010-07-27
Hi Experts,

I am trying to write the following code and have some trouble.  

#include <stdio.h>
typedef struct {
      int a;
      int b;
}Box;

void main()
{
    Box *myBox;
    myBox->a = 4;
}

I am getting this warning message when I compile: "local variable 'myBox' used without having been initialized "  and the program crashes when I run it.

How can I solve this problem?  How do I initialize this pointer??

Thanks
0
Comment
Question by:g_jey
1 Comment
 
LVL 4

Accepted Solution

by:
n_fortynine earned 50 total points
ID: 9631176
what you have there is a pointer. without having been initialized means that it is not given any memory, i.e. the instance of your struct Box has not been created. so you have to do:
Box* myBox;
myBox = new Box;
then you can use it all you want.
just remember to delete it after you're done with the instance
delete myBox;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 97
Performance degradation with linux shared library 6 135
Is string constant address ? 10 192
C++ vs C compilers 13 157
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now