🌐
W3Schools
w3schools.com › c › c_pointers.php
C Pointers
You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator): int myAge = 43; // Variable declaration int* ptr = &myAge; // Pointer declaration // Reference: Output the memory address of ...
🌐
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
🌐
SDSU
edoras.sdsu.edu › doc › c › pointers-1.2.2
Everything you need to know about pointers in C
These two declarations are not equivalent: ... In the first example, the int (i.e. **ptr_a) is const; you cannot do **ptr_a = 42. In the second example, the pointer itself is const; you can change **ptr_b just fine, but you cannot change (using pointer arithmetic, e.g.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › pointer-declarations
Pointer Declarations | Microsoft Learn
January 25, 2023 - The declaration of w specifies that the program can't change the value pointed to and that the program can't modify the pointer. struct list *next, *previous; /* Uses the tag for list */ This example declares two pointer variables (next and ...
🌐
Scaler
scaler.com › home › topics › pointer declaration in c
Pointer Declaration in C - Scaler Topics
July 11, 2024 - In the example above, we have done a pointer declaration and named ptr1 with the data type integer. ... 22 ways of initializing a pointer in C once the pointer declaration is done.
🌐
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
🌐
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....
🌐
IncludeHelp
includehelp.com › c › pointers-declarations-in-c-programming-language.aspx
Pointers Declarations in C programming language
The character asterisk (*) tells to the compiler that the identifier ptr should be declare as pointer. The data type int tells to the compiler that pointer ptr will store memory address of integer type variable.
🌐
freeCodeCamp
freecodecamp.org › news › pointers-in-c-programming
How to Use Pointers in C Programming
May 3, 2023 - This declares three pointer variables named "p", "q", and "r" that can hold the memory address of an integer. When we declare a pointer variable, it does not automatically point to any particular memory location. To initialize a pointer to point to a specific variable or memory location, we use the ampersand & operator to get the address of that variable. For example, to initialize the pointer p to point to an integer variable called x, we would write:
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?
🌐
Cppreference
en.cppreference.com › w › c › language › pointer.html
Pointer declaration - cppreference.com
April 9, 2024 - 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; ...
🌐
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.
🌐
Javatpoint
javatpoint.com › c-pointers
C Pointers - javatpoint
July 8, 2016 - The sizeof() operator contains ... same type as specified in the pointer declaration. For example, if we declare the int pointer, then this int pointer cannot point to the float variable or some other type......
🌐
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.
🌐
Medium
lorenzopiombini.medium.com › pointers-in-c-2ad210278a51
Pointers in C. a Beginner guide. | by Lorenzo Piombini | Medium
September 13, 2024 - /* I like this declaration better than the oone showed earlier */ int *pNumb = NULL, numb = 0; int *pNumb2 = NULL, numb2 = 2; /* However i think the follwing is more readable as it strikes your eyes better and clearly*/ int *pNumb = NULL; int numb = 0; int *pNumb2 = NULL; int numb2 = 2; To help us using pointers safely is good practice before access a pointer variable, to check whether a pointer is NULL, meaning it doesn’t point to anything, so if it is not NULL we can use the pointer, or if it is NULL we can then handle the situation accordingly.
🌐
Yale University
cs.yale.edu › homes › aspnes › pinewiki › C(2f)Pointers.html
C/Pointers
Declaring a pointer-valued variable allocates space to hold the pointer but not to hold anything it points to. Like any other variable in C, a pointer-valued variable will initially contain garbage---in this case, the address of a location that might or might not contain something important.
🌐
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.
🌐
Unstop
unstop.com › home › blog › pointers in c | ultimate guide with easy explanations (+code)
Pointers In C | Ultimate Guide With Easy Explanations (+Code)
March 21, 2024 - Let's examine an example to understand this concept better: int number = 88; // An integer variable with a value int *pNumber; // Declare a pointer variable called pNumber, which points to an integer pNumber = &number; // Assign the address ...
🌐
guvi.in
studytonight.com › c › declaring-and-initializing-pointer.php
Declaring and Initializing Pointers in C
September 17, 2024 - Here, pointer_name is the name of the pointer and that should be a valid C identifier. The datatype of the pointer and the variable to which the pointer variable is pointing must be the same. Following are some examples of declaring a pointer in C: