yh c
yh c

Reputation: 95

What's the difference between "int a, b =0" and "int a =0; int b = 0" in c++?

When I implement the function, I originally define two variables 'p' and 'result' in one line.

void huffman(Node** nodeArray, int n)
{
    int p,result = 0;  
    cout << p << endl;
    sort(nodeArray, p, n); //sort
    while(p < n-1){
        Node* newNode = new Node;
        newNode->fre = nodeArray[p]->fre + nodeArray[p+1]->fre;
        p++;
        result += newNode->fre; 
        nodeArray[p] = newNode;
        sort(nodeArray, p, n);
    } 
    cout << result << endl;
}

There are some bugs with the whole codes, so I add a line to test if 'p' is correct after defining. But the output is 1, which is wrong. Afterwards, I try to define two variables in two lines. `

void huffman(Node** nodeArray, int n)
{
    int p = 0;
    int result = 0;   
    cout << p << endl;
    sort(nodeArray, p, n); //sort
    while(p < n-1){
        Node* newNode = new Node;
        newNode->fre = nodeArray[p]->fre + nodeArray[p+1]->fre;
        p++;
        result += newNode->fre; 
        nodeArray[p] = newNode;
        sort(nodeArray, p, n);
    } 
    cout << result << endl;
}

The output for 'p' is 0, which is correct. But I don't know why there is difference.

Upvotes: 3

Views: 10143

Answers (4)

Muse Icky
Muse Icky

Reputation: 1

Another observation, although it may be a little unrelated is:

int * a = NULL, b = NULL;

This is also erroneous as b gets defined as int data type instead of int *.

So always make sure that while defining and assigning values to pointers in one go, you should either separate out the initialization like so:

int * a = NULL; int * b = NULL;

Or simply write:

int * a  = NULL, * b = NULL;

I hope this helps.

Upvotes: 0

M Umer
M Umer

Reputation: 413

you can do something like that:

int a,b;
a = b = 0;

the line

int a,b=0; 

is just like

int a; int b = 0;

Upvotes: 3

Zaid Khan
Zaid Khan

Reputation: 367

int p,result = 0;

It simply means that you have just declared a variable p, but you have declared and defined variable result. Some people also call it initialization (case of result).

Difference between declaration and definition of the variables:

Variable declaration refers to the part where a variable is first declared or introduced before its first use. Variable definition is the part where the variable is assigned a memory location and a value. Most of the times, variable declaration and definition are done together.

And now coming to you question i.e it doesn't matter if you declare two separate variables of same type in one line or not until you define them. It means It is not difference of line which giving you the different answer.

Now Let's take a look.

In first example when you write int p you just declared the variable. You not defined it or initialized it so you can't predict the value of p because it in this case it depends on the compiler. In most compilers it shows 0.

But In the second example you have defined the p and result both it means that p is showing the value 0 as per your intentions.

And the thing you saying that p is giving 1 as output..Are you sure that you are telling us value of p instead of result ?

Upvotes: 3

Ami Tavory
Ami Tavory

Reputation: 76346

The line

int a, b = 0;

is equivalent to

int a;
int b = 0;

So, relative to

int a = 0;
int b = 0;

the difference is that a is not initialized.


In your specific code, since you have things like

while(p < n-1){

the execution is not determinable - p has not had an initial value set.

Upvotes: 7

Related Questions