These two “methods” do exactly the same thing. And as you said, the second one is just a compound literal.

struct  Object obj1 = { .id = 1 };
struct  Object *obj1_p = &obj1;

// The same, just in a compound literal?
struct  Object *obj2_p = &(struct Object){ .id = 1 };

This allocates enough memory for struct Object without initializing it. And no you don't need to cast it, because malloc returns void *, which is automatically and safely promoted to any other pointer. But if you do, you should cast it to struct Object* instead of Object*.

struct Object *obj3_p = (struct Object*) malloc(sizeof(struct Object));

That looks very bulky though... My preferred way of doing it is this:

struct Object *obj3_p = malloc(sizeof *obj3_p);
Answer from Andy Sukowski-Bang on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-pointers
Pointers in C - GeeksforGeeks
It is the backbone of low-level memory manipulation in C. A pointer is declared using its data type followed by an asterisk (*) and the pointer name, indicating the type of data it can store the address of.
Published: 2 weeks ago
🌐
Scaler
scaler.com › home › topics › pointer declaration in c
Pointer Declaration in C - Scaler Topics
July 11, 2024 - Choose from our industry-leading programs designed for career success ... Explanation: For pointer declaration in C, you must make sure that the data type you're using is a valid C data type and that the pointer and the variable to which the pointer variable points must have the same data type.
🌐
SDSU
edoras.sdsu.edu › doc › c › pointers-1.2.2
Everything you need to know about pointers in C
This is also a comment. This is output you’d see on your screen. ... A pointer is a memory address. ... Say you declare a variable named foo. ... This variable occupies some memory. On a PowerPC, it occupies four bytes of memory (because an int is four bytes wide).
🌐
W3Schools
w3schools.com › c › c_pointers.php
C Pointers
When used in declaration (int* ptr), it creates a pointer variable.
🌐
IncludeHelp
includehelp.com › c › pointers-declarations-in-c-programming-language.aspx
Pointers Declarations in C programming language
Pointer declaration is similar to other type of variable except asterisk (*) character before pointer variable name. ... ptr is the name of pointer variable (name of the memory blocks in which address of another variable is going to be stored).
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_pointers.htm
Pointers in C
With pointers, you can access and ... like linked lists, trees, and graphs. To declare a pointer, use the dereferencing operator (*) followed by the data type....
🌐
cppreference.com
en.cppreference.com › c › language › pointer
Pointer declaration - cppreference.com
The qualifiers that appear between * and the identifier (or other nested declarator) qualify the type of the pointer that is being declared: int n; const int * pc = &n; // pc is a non-const pointer to a const int // *pc = 2; // Error: n cannot be changed through pc without a cast pc = NULL; // OK: pc itself can be changed int * const cp = &n; // cp is a const pointer to a non-const int *cp = 2; // OK to change n through cp // cp = NULL; // Error: cp itself cannot be changed int * const * pcp = &cp; // non-const pointer to const pointer to non-const int
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › pointer-declarations
Pointer Declarations | Microsoft Learn
January 25, 2023 - Access to this page requires authorization. You can try changing directories. ... A pointer declaration names a pointer variable and specifies the type of the object to which the variable points.
Find elsewhere
Top answer
1 of 4
2

These two “methods” do exactly the same thing. And as you said, the second one is just a compound literal.

struct  Object obj1 = { .id = 1 };
struct  Object *obj1_p = &obj1;

// The same, just in a compound literal?
struct  Object *obj2_p = &(struct Object){ .id = 1 };

This allocates enough memory for struct Object without initializing it. And no you don't need to cast it, because malloc returns void *, which is automatically and safely promoted to any other pointer. But if you do, you should cast it to struct Object* instead of Object*.

struct Object *obj3_p = (struct Object*) malloc(sizeof(struct Object));

That looks very bulky though... My preferred way of doing it is this:

struct Object *obj3_p = malloc(sizeof *obj3_p);
2 of 4
2

I wrote this piece of code, hope it helps you to better understand some features of pointers:

#include <stdio.h>
#include <stdlib.h>

struct Object { int id; };

struct Object *getObjectNaive() {
    struct Object* obj2_p = &(struct Object) { .id = 2 };

    return obj2_p; // UB: Returns the address of a local object (the compound literal).
}

struct Object *getObject() {
    struct Object* obj3_p = malloc(sizeof(*obj3_p)); // Better way of calling malloc than using sizeof(struct Object).
    obj3_p->id = 3; // You don't need to do this.

    return obj3_p; // This needs to be freed later on!
}

int main(void) {
    struct Object obj1 = { .id = 1 };
    struct Object* obj1_p = &obj1;
    
    printf("obj1.id = %d\n", obj1_p->id); 
    obj1_p->id = 10; // You can change values using the pointer
    printf("obj1.id = %d\n", obj1_p->id); 

    // The only different thing with this case is that you don't
    // "lose" your object when setting the pointer to NULL 
    // (although you can only access it through the object, not through the pointer).

    obj1_p = NULL;
    printf("obj1.id = %d\n", obj1_p->id); // This won't work (undefined behaviour).
    printf("obj1.id = %d\n", obj1.id); // This will.


    struct Object* obj2_p = &(struct Object) { .id = 1 };
    obj2_p->id = 2; // You can change the id
    printf("obj2.id = %d\n", obj2_p->id);

    // If you make this pointer point to another address, you "lose" your object.
    obj2_p = NULL;
    printf("obj2.id = %d", obj2_p->id); // This won't work at all (undefined behaviour).


    // Both of these pointers point to objects in the stack, so, for example,
    // they don't work when returning from a function.
    obj2_p = getObjectNaive();
    obj2_p->id = 20; // This won't work (undefined behaviour).
    printf("obj2.id = %d\n", obj2_p->id); // This works if you don't dereference the pointer.


    // The third case is not the same as the other two, since you are allocating memory on the heap.
    // THIS is a time where you can only use one of these three methods.
    struct Object *obj3_p = getObject(); // This works!
    printf("obj3.id = %d\n", obj3_p->id);
    obj3_p->id = 30; // This works now.
    printf("obj3.id = %d\n", obj3_p->id);

    free(obj3_p); // You need to do this if you don't want memory leaks.

    return 0;
}

This is the output when commenting out undefined behaviour:

obj1.id = 1
obj1.id = 10
obj1.id = 10
obj2.id = 2
obj2.id = 2
obj3.id = 3
obj3.id = 30

I'd recommend you to check out these links, they turned out to be pretty helpful for me:

  • Returning a pointer from a function
  • What and where are the stack and heap?
  • What EXACTLY is meant by “de-referencing a NULL pointer”?
  • Why dereferencing a null pointer is undefined behaviour?
  • Do I cast the result of malloc?
🌐
freeCodeCamp
freecodecamp.org › news › pointers-in-c-programming
How to Use Pointers in C Programming
May 3, 2023 - In C, a pointer is simply a variable that holds a memory address. We can think of it as a way to refer to a specific location in memory. To declare a pointer variable in C, we use the asterisk * symbol before the variable name.
🌐
Medium
medium.com › @Dev_Frank › pointers-in-c-422cccdbf2f6
POINTERS IN C. Pointer is a variable that stores the… | by Dev Frank | Medium
February 16, 2024 - The declaration involves using the ( * ) dereference operator preceding the pointer’s name. When we want to use a pointer, we need to tell the computer that we’ll be using one.
🌐
Programiz
programiz.com › c-programming › c-pointers
C Pointers (With Examples)
Let's take an example. ... Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer. To get the value of the thing pointed by the pointers, we use the * operator. For example: int* pc, c; c = 5; pc = &c; printf("%d", *pc); // Output: 5
🌐
Medium
lorenzopiombini.medium.com › pointers-in-c-2ad210278a51
Pointers in C. a Beginner guide. | by Lorenzo Piombini | Medium
September 13, 2024 - in the code above, the first two pointers can contain only memory addresses of type int (integer numbers), in the last statement we have a pointer that can contain only memory addresses of double type. As you can see, all these three declarations are valid in C, the * operator is in a different position each statement, and that’s fine, the compiler won’t complain, it better to use NULL instead of the number 0 (zero), because this improve readability of the program(NULL is defined in <stdio.h>)
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Declaring-Function-Pointers.html
Declaring Function Pointers (GNU C Language Manual)
For instance, int (*a) (); says, “Declare a as a pointer such that *a is an int-returning function.” ... /* Declare a function returning char *. */ char *a (char *); /* Declare a pointer to a function returning char.
🌐
Filo
askfilo.com › cbse › smart solutions › how to declare a pointer in c
How to declare a pointer in C... | Filo
May 10, 2025 - To declare a pointer in C, use the syntax: <type> *<pointer_name>; For example, int *ptr; declares a pointer to an integer.
🌐
Substack
alexanderobregon.substack.com › alexander obregon's substack › pointers in c for beginners
Pointers in C for Beginners - Alexander Obregon's Substack
January 12, 2026 - C declarations follow a rule where the base type appears on the left and the more detailed information gathers around the variable name. That means the same syntax can describe a single pointer, a pointer to a pointer, an array of pointers, a pointer to an array, or a pointer to a function, all by rearranging stars, brackets, and parentheses.
🌐
BYJUS
byjus.com › gate › pointers-in-c
Pointers in C
August 1, 2022 - Normally, the declaration of a pointer would take a form like this: data_type * name_of_pointer_variable; ... The data_type refers to this pointer’s base type in the variable of C.
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Pointers.html
C/Pointers
The expression a[n] is defined to be equivalent to *(a+n); the index n (an integer) is added to the base of the array (a pointer), to get to the location of the n-th element of a. The implicit * then dereferences this location so that you can read its value (in a normal expression) or assign to it (on the left-hand side of an assignment operator). The effect is to allow you to use a[n] just as you would any other variable of type int (or whatever type a was declared as).
🌐
guvi.in
studytonight.com › c › declaring-and-initializing-pointer.php
Declaring and Initializing Pointers in C
September 17, 2024 - When we declare a pointer, it contains garbage value, which means it could be pointing anywhere in the memory. Pointer Initialization is the process of assigning the address of a variable to a pointer.