0

What's the difference between 1 and 2?

  1. int *p;
    p = malloc(2 * sizeof(int));
    
  2. int *p = malloc(2 * sizeof(int));
    

The way I first learned it is number 1, but I've seen someone do it number 2. Is number 1 and 2 the same thing? I'm assuming it is, but I'm confused because I don't understand why number 2 is

int *p = malloc(2 * sizeof(int));

not

int p = malloc(2 * sizeof(int));

The way he used it was for array. Later in the code, he used p to save some desired values.

Like this.

p[0] = i;
p[1] = j;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • You're allocating a pointer, so the type must be `int *p` in all cases. That allows you to use `p[0]` etc too. The one-liner is generally the better way to write the code. – Jonathan Leffler Jun 21 '22 at 03:51

2 Answers2

1

(1) and (2) are the same which are used to initialize pointer with memory allocation.

I think you're confused with the way to initialize pointer, not how to use malloc(). Take a look on some articles about "Initialize pointer" like this one.

int number = 88;     // An int variable with a value
int * pNumber;       // Declare a pointer variable called pNumber pointing to an int (or int pointer)
pNumber = &number;   // Assign the address of the variable number to pointer pNumber
 
int * pAnother = &number; // Declare another int pointer and init to address of the variable number

int p = malloc(2 * sizeof(int));

This way is not correct to declare a pointer, in this case, p is just an integer variable, not a pointer that points to an integer variable.

dustin2022
  • 182
  • 2
  • 18
  • 1
    *(1) and (2) are the same which are used to initialize pointer with memory allocation.* Not quite true. Initialization and assignment are not the same in C. `int *p = malloc(2 * sizeof(int));` *initializes* `p` with the value returned by `malloc()`, but `int *p; p = malloc(2 * sizeof(int));` *assigns* the value returned by `malloc()` to `p`. That won't make any difference here, but initialization is not the same as assignment, and there are numerous constructs where it does matter. – Andrew Henle Jun 21 '22 at 15:28
  • 1
    See [**Is there a difference between initializing a variable and assigning it a value immediately after declaration?**](https://stackoverflow.com/questions/16217839/is-there-a-difference-between-initializing-a-variable-and-assigning-it-a-value-i) for some examples. – Andrew Henle Jun 21 '22 at 15:29
0

What's the difference between 1 and 2?

1 and 2 do the same thing. The compiler should produce exactly the same code. They both declare a variable p of type int * and then initialize p with the pointer returned from malloc.

It's int *p = ... because int * is the type. The variable is p. It is declaring the variable p as the type int * and then assigning to p.

The difference is that 2 does it in a single statement. This is preferable because it ensures the variable is initialized, and it makes it easier to see that the variable is initialized. Uninitialized variables contain garbage and are a common source of errors. It's usually a good idea to initialize every variable immediately, even if it's to 0 or NULL.

You will often see code which declares and initializes variables separately; sometimes all the variables are declared at the top of each function. This is an old style from way back when C required you to declare all your variables at the start of a function. This is no longer an issue. You can declare variables as needed now.

Schwern
  • 153,029
  • 25
  • 195
  • 336