What is the difference between declaration and initialization

In programming, declaration and initialization are two different concepts.

Declaration means declaring the name and type of a variable or function in a program so that they can be used in the program. For example, the following code declares an integer variable named "count":

int count;

In this example, we're just telling the compiler that we need an integer variable called "count", but we haven't assigned a value to it yet.

Initialization means assigning an initial value to a variable when it is declared. For example, the following code declares and initializes an integer variable named "count":

int count = 0;

In this example, we declare an integer variable called "count" and initialize it to 0. This means that we have assigned an initial value to the variable when we declared it.

In general, declaration means to tell the compiler that we need a variable or function, and initialization means to assign an initial value to the variable or function. In some cases, declaration and initialization can happen at the same time, but they are two different concepts.


Declaration and initialization of pointer variables

Yes, the same is true for pointer variables. When declaring a pointer variable, we're just telling the compiler that we need a pointer variable that points to a certain type, but we haven't allocated memory for it or pointed to any address. For example, the following code declares an integer pointer variable named "ptr":

int *ptr;

In this example, we're just telling the compiler that we need a pointer variable to an integer, but we haven't allocated memory for it or pointed to any address yet.

When initializing a pointer variable, we allocate memory for it and point it to a certain address. For example, the following code declares and initializes an integer pointer variable named "ptr":

int *ptr = NULL;

In this example, we declare an integer pointer variable named "ptr" and initialize it to NULL. This means that we have allocated memory for the variable when we declared it, and pointed it to the NULL address.

In general, for pointer variables, declaration means telling the compiler that we need a pointer variable pointing to a certain type, and initialization means allocating memory for the pointer variable and pointing it to a certain address.

Guess you like

Origin blog.csdn.net/k1419197516/article/details/129958730