Declare and initialize pointer concisely (i. e. pointer to int)

String literals are a corner case : they trigger the creation of the literal in static memory, and its access as a char array. Note that the following doesn't compile, despite 42 being an int literal, because it is not implicitly allocated :

int *p = &42;

In all other cases, you are responsible of allocating the pointed object, be it in automatic or dynamic memory.

int i = 42;
int *p = &i;

Here i is an automatic variable, and p points to it.

int * i;
*i = 42;

You just invoked Undefined Behaviour. i has not been initialized, and is therefore pointing somewhere at random in memory. Then you assigned 42 to this random location, with unpredictable consequences. Bad.

int *i = malloc(sizeof *i);

Here i is initialized to point to a dynamically-allocated block of memory. Don't forget to free(i) once you're done with it.

int i = 42, *p = &i;

And here is how you create an automatic variable and a pointer to it as a one-liner. i is the variable, p points to it.

Edit : seems like you really want that variable to be implicitly and anonymously allocated. Well, here's how you can do it :

int *p = &(int){42};

This thingy is a compound literal. They are anonymous instances with automatic storage duration (or static at file scope), and only exist in C90 and further (but not C++ !). As opposed to string literals, compound literals are mutable, i.e you can modify *p.

Edit 2 : Adding this solution inspired from another answer (which unfortunately provided a wrong explanation) for completeness :

int i[] = {42};

This will allocate a one-element mutable array with automatic storage duration. The name of the array, while not a pointer itself, will decay to a pointer as needed.

Note however that sizeof i will return the "wrong" result, that is the actual size of the array (1 * sizeof(int)) instead of the size of a pointer (sizeof(int*)). That should however rarely be an issue.


int i=42;
int *ptr = &i;

this is equivalent to writing

int i=42;
int *ptr;
ptr=&i;

Tough this is definitely confusing, but during function calls its quite useful as:

void function1()
{
int i=42;
function2(&i);
}

function2(int *ptr)
{
printf("%d",*ptr); //outputs 42
}

here, we can easily use this confusing notation to declare and initialize the pointer during function calls. We don't need to declare pointer globally, and the initialize it during function calls. We have a notation to do both at same time.

int *ptr; //declares the pointer but does not initialize it
//so, ptr points to some random memory location
*ptr=42; //you gave a value to this random memory location

Though this will compile, but it will invoke undefined behaviour as you actually never initialized the pointer.

Also,

char *ptr;
char str[6]="hello";
ptr=str;

EDIT: as pointed in the comments, these two cases are not equivalent. But pointer points to "hello" in both cases. This example is written just to show that we can initialize pointers in both these ways (to point to hello), but definitely both are different in many aspects.

char *ptr;
ptr="hello";

As, name of string, str is actually a pointer to the 0th element of string, i.e. 'h'. The same goes with any array arr[], where arr contains the address of 0th element.