Declaring variable (difference between int c; and int c=new int();

renato

What is the difference between:

int c; int c=new int();

Sometimes I use the first one and it works perfectly, but sometimes when I use inside a loop it doesn´t work.

this work:

    public int[] junta(Table ent,int j)
    {
         int[] matriz=new int[count(ent)];
         int k;
         k = 0;
         for (int i = fline(ent); i <= count(ent) + 1; i++)
         {
             if (Convert.ToString(ent.Cells[j, 3].Value) == Convert.ToString(ent.Cells[i, 3].Value))
             {

                 matriz[k]=Convert.ToInt32(ent.Cells[i,0].Value);
                 k++;

             }

         }
   }

this doesn´t work:

public int[] junta(Table ent,int j)
{
     int[] matriz;
     int k;
     k = 0;
     for (int i = fline(ent); i <= count(ent) + 1; i++)
     {
          if (Convert.ToString(ent.Cells[j, 3].Value) == Convert.ToString(ent.Cells[i, 3].Value))
          {

               matriz[k]=Convert.ToInt32(ent.Cells[i,0].Value);
               k++;

           }

      }
}
Lasse V. Karlsen

The main difference is that this:

int c;

does not assign a value to the variable, so if you try to access the variable afterwards the compiler will complain with this:

Use of unassigned local variable 'c'

However if you do this:

int c = new int();

then you might as well write this:

int c = 0;

and this will assign a value to it.

You can test the following two code snippets and observe their difference:

int c;
int a = c;

with this:

int c = 0; // try = new int(); as well if you want to
int a = c;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between int and new int()

From Dev

Difference Between int (*)(...) and int (^)(...) in OS X C?

From Dev

Difference Between int (*)(...) and int (^)(...) in OS X C?

From Dev

What is the difference between int and (int) in C?

From Dev

What is the difference between "int *a = new int" and "int *a = new int()"?

From Dev

What is the difference between “int *a = new int” and “int *a = new int [5]"?

From Dev

Difference between char and int when declaring character

From Dev

Declaring my int array in C

From Dev

What's the difference between the types - int * and int *[100] in C?

From Dev

Difference between '(int)a ', 'a as int' and Convert.ToInt32(a) in C#

From Dev

What's the difference between the types - int * and int *[100] in C?

From Dev

Difference between '(int)a ', 'a as int' and Convert.ToInt32(a) in C#

From Dev

How to tell the difference between typeof(int) == typeof(int?) in C#

From Dev

What is the difference between int x_ and int x in C++

From Dev

In C++ what is the difference between (int *) and &?

From Dev

Difference between malloc and (int *)malloc in C

From Dev

What is the difference between "short int" and "short" in c?

From Dev

Difference between Ctrl-C and kill -INT?

From Dev

Difference between int * array[60] and int * array = new int(60);

From Dev

Difference between int * array[60] and int * array = new int(60);

From Dev

The difference between int(*function)(int,int) and int*function(int,int)

From Dev

Short int variable in c

From Dev

Short int variable in c

From Dev

difference between typecasts (int**) and int(*)

From Dev

Difference between Int... and Int[]

From Dev

Difference between new HashMap(int) and guava Maps.newHashMapWithExpectedSize(int)

From Dev

C unsigned int + int

From Dev

Difference between Short, long and Long long int in C programming?

From Dev

What is the difference between int &r=x and r=y in C++?

Related Related

  1. 1

    Difference between int and new int()

  2. 2

    Difference Between int (*)(...) and int (^)(...) in OS X C?

  3. 3

    Difference Between int (*)(...) and int (^)(...) in OS X C?

  4. 4

    What is the difference between int and (int) in C?

  5. 5

    What is the difference between "int *a = new int" and "int *a = new int()"?

  6. 6

    What is the difference between “int *a = new int” and “int *a = new int [5]"?

  7. 7

    Difference between char and int when declaring character

  8. 8

    Declaring my int array in C

  9. 9

    What's the difference between the types - int * and int *[100] in C?

  10. 10

    Difference between '(int)a ', 'a as int' and Convert.ToInt32(a) in C#

  11. 11

    What's the difference between the types - int * and int *[100] in C?

  12. 12

    Difference between '(int)a ', 'a as int' and Convert.ToInt32(a) in C#

  13. 13

    How to tell the difference between typeof(int) == typeof(int?) in C#

  14. 14

    What is the difference between int x_ and int x in C++

  15. 15

    In C++ what is the difference between (int *) and &?

  16. 16

    Difference between malloc and (int *)malloc in C

  17. 17

    What is the difference between "short int" and "short" in c?

  18. 18

    Difference between Ctrl-C and kill -INT?

  19. 19

    Difference between int * array[60] and int * array = new int(60);

  20. 20

    Difference between int * array[60] and int * array = new int(60);

  21. 21

    The difference between int(*function)(int,int) and int*function(int,int)

  22. 22

    Short int variable in c

  23. 23

    Short int variable in c

  24. 24

    difference between typecasts (int**) and int(*)

  25. 25

    Difference between Int... and Int[]

  26. 26

    Difference between new HashMap(int) and guava Maps.newHashMapWithExpectedSize(int)

  27. 27

    C unsigned int + int

  28. 28

    Difference between Short, long and Long long int in C programming?

  29. 29

    What is the difference between int &r=x and r=y in C++?

HotTag

Archive