Question: Well, I am new to C. I would like to know as my title says.

Suppose I declare pointers as following,

```
char *chptr1;
char **chptr2;
int *i;
int **ii;
struct somestruct *structvar1;
struct somestruct **structvar2;

```

Then,

1. Do I need to allocate memory for every variable, before storing data into them?
2. Is there any special case when I do not need to allocate memory for them? for this I know one for `char` pointer,
`strdup()` which allocate memory itself, we have not to care much about it.
3. Any further suggestions are welcome.

Answer: 
> 
> Do I need to allocate memory for every variable, before storing data into them?
> 
> 
> 

It depends on the use case.

Let's assume you have aprogram where you can enter a user name, but if no user name is entered, a hardcoded value "Default User" is shown. Now you want to pass arround that name. If the user entered a name you might have allocted some space for it, and pass aorund that pointer. If no user name is given you are pointing to that hard coded value, which you already predefined, and you don't need to allocate memory for it.

```
 char const *Default = "Default User"
 char *username = NULL;
 username = getUser();   // here the name is allocated dynamically if it is present.
 if(username == NULL)
    username = Default;

```

In the above case, the sapce for the "Default User" String is already reserved in the executable by the compiler, so you need not to allocate additional memory for it, unless you want to manipulate it.

Another case could be when you program a low level device, and you have a pointer to a hardware buffer provided by the device. In such a case you also wouldn't allocate memory, but you would still use the pointer for that buffer which may be on a fixed address. So you would use the pointer like this (Example which would change the screen border color on a C64):

```
 char *BorderColor = 0xd020;
 *BorderColor = 0;

```
I think that in C, a variable is a pointer added \* with a memory. And a pure pointer has no memory. So if we want to store a data, we need to give the pointer a memory.
Sure, there are many ways to assign the memory.

First:

```
uint32_t x;
uint32_t *p = &x;
uint32_t array[10];
uint32_t *q = array;// equals to int *q = &array[0];

```

Second:

```
#define NUM_MACRO   ((uint32_t) 10)
//we must free after we using. Or, perhaps we we will get a segment fault.
uint32_t *p = malloc(size(uint32_t) * NUM_MACRO); 

```