C++ Uninitialized Local Variable

uninitialized local variable used C ++

I see what your problem is. You haven't initialized your char variables.

char a = 'a'; 
char b = 'b';
char c = 'c';

uninitialized local variable used c++

Why can't I initialize the integer variable num with the value of the number field of the Strct structure?

Because the pointer is uninitialised, and thus it doesn't point to any object. Indirecting through the pointer, or even reading the value of the pointer result in undefined behaviour.

I thought my strct points to the Strct structure, that is, to its type

No. Pointers don't point to types. Object pointers point to objects. Types are not objects in C++.

16.0f is not the value of number. 16.0f is the default member initialiser of that member. If you create an object of type Strct, and you don't provide an initialiser for that member, then the default member initialiser will be used to initialise the member of the object in question.

then can I define a member function that returns the address of this structure?

A structure is a type. A type isn't stored in an address. There is no such thing as "address of a type".


Here is an example of how to create a variable that names an instance of the class Strct:

Strct strct;

You can access the member of this variable using the member access operator:

int num = strct.number;

Here is an example of how to create an instance that doesn't use the default member initialiser:

Strct strct = {
.number = 4.2f,
};

Uninitialized local variable c4700

If you just want to make the compiler happy and avoid the warning, initialize p before doing anything with it:

Person p = {};  // zero's out all the members (default initializes)

But the real issue is that you are passing an uninitialized instance of Person by value (a copy of p) into getData. The compiler wants to assume that objects passed by value are initialized - because most functions aren't written to expect uninitialized parameters.

And getData is returning another copy of Person back. While compilers have gotten efficient in recent years, it's still better to pass by reference.

void getData(Person& p) {

cout << "Enter Full name: ";
cin.get(p.name, 50);

cout << "Enter age: ";
cin >> p.age;

cout << "Enter salary: ";
cin >> p.salary;
}

Then simply invoke as:

getData(p); 

The same instance of Person that's declared in main gets passed and returned from getData. As you have it now, multiple copies of Person are made along the way.

error C4700: uninitialized local variable 'c' used ,

In your code your declare a variable c. Then the first thing you try to do with the variable is print it's value. But you have never given it a value so the compiler gives you an 'uninitialised local variable' error. That's what it means, you are trying to use the value of a variable before you have given the variable a value.

I'm guessing that you meant to give c the value of the top item on the stack. If so then you should have written this code

StackItemType c ;
stack stack(5);
stack.push('a');
stack.push('b');
stack.push('c');
stack.pop(&c); // <<--- new code here that gives c a value
cout << c;
cout << endl;
system("pause");

How to solve uninitialized local variable used?

The problem you are facing is cause by your use of the variable counter without initializing it. This is exactly what the compiler is telling you.

When you try to execute counter=counter++; for the first time, counter has no definied value. You might think by int counter; it gets initialized with 0, but this is wrong in C.

The next problem is the line counter=counter++; itself. If you want to increment counter, just use counter++. Depending on the compile you are using, the use of counter=counter++ should give you at least a warning. On my machine using Apple LLVM version 8.1.0 (clang-802.0.42) I get

warning: multiple unsequenced modifications to 'counter' [-Wunsequenced]

Then you try to loop until you read 0. But scanf() (use this instead of the Microsoft specific scanf_s()) does not return what it has read from stdin but the number of input items assigned. It returns 0 in the event of a matching failure.

So here is what to do:

  1. Initialize counter
  2. Replace counter=counter++ with counter++
  3. Use another loop breaking condition than the return value of scanf()

One approach could be the following:

#include <stdio.h>

int main() {
int x=0;
int counter=0;

do{
printf("Enter the a signed number: ");
scanf("%d", &x);
if (!(x % 2)){
counter++;
}
} while(x);

printf(" %d pair numbers\n", counter);
}

Are uninitialized local variables in C static by default?

No, they are not static by default. In principle, the initial value can be anything at all. Using the value could even be undefined behaviour. In practice, the compiler picks a memory location for the variable on the stack, and the variable's initial value is whatever happens to already be in that memory location.

Since you don't run any other code between the first up() and the second up(), in practice your program is likely to pick the same location twice and therefore it still has the previous value. If you called another function in between, that function's local variables would go in the same space previously used by up()'s local variables, which would overwrite the value from the first up().

You certainly can't rely on it. Even if you don't call any other functions in-between, the compiler might add one "secretly" (for various reasons). Or the compiler may decide to adjust the stack between the two calls to up so each call might get a different stack location for its local variables.

You also aren't guaranteed that the first value is 0. Because it's whatever happens to be at that memory location already, it could be something left over from a previous function. main isn't the first function that gets called; there is some function in the standard library which does set-up work before it calls main.



Related Topics



Leave a reply



Submit