The actual power that C lies in pointers. The pointers room slightly challenging to grasp at first. After going with the basics that pointers, friend will acquire a better idea around what they are and also how to use them.

You are watching: How many bytes is a pointer in c

What is a Pointer? #

A pointer is a variable supplied to store a memory address. Let's first learn exactly how memory is arranged inside a computer.

Memory in a computer is comprised of bytes (A byte is composed of 8 bits) i ordered it in a sequential manner. Every byte has a number connected with it just like index or subscript in an array, i m sorry is called the address of the byte. The address of byte starts from 0 to one less than the size of memory. Because that example, say in a 64MB of RAM, there space 64 * 2^20 = 67108864 bytes. Because of this the deal with of this bytes will begin from 0 come 67108863.


Let's watch what happens once you declare a variable.

As we know an int occupies 4 bytes that data (assuming we room using a 32-bit compiler) , so compiler to make reservation 4 continually bytes from memory to store an essence value. The address of the an initial byte the the 4 allocated bytes is known as the attend to of the variable marks. Let's to speak that resolve of 4 continually bytes are 5004, 5005, 5006 and also 5007 then the address of the change marks will be 5004.


Address Operator (&) #

To uncover the deal with of a variable, C offers an operator called address operator (&). To discover out the deal with of the variable marks we need to location & operator in front of it, prefer this:

The following program demonstrates exactly how to usage the deal with operator (&).

1 2 3 4 5 6 7 8 91011121314
// program to demonstrate address(&) operator#includeint main() int ns = 12; printf("Address of ns = %u \n", &i); printf("Value of ns = %d ", i); // signal to operating mechanism program ran fine return 0;
Expected Output:

Address of i = 2293340Value of i = 12
Note: deal with of i may vary every time you operation the program.

How that works:

To uncover the deal with of the variable, head the variable name by & operator. An additional important thing to notice about the program is the usage of %u switch specification. Recall that %u counter specification is provided to publish unsigned decimal numbers and since the storage addresses can't it is in negative, girlfriend must always use %u rather of %d.

Address the operator (&) can't be supplied with constants or expression, it can only be used with a variable.

&var; // ok&12; // error because we space using & operator with a constant&(x+y) // error due to the fact that we space using & operator with an expression
We have actually been using deal with operator(&) in the duty scanf() without learning why? The resolve of a change is detailed to scanf(), so that it knows where to create data.

Declaring reminder Variables #

As currently said a reminder is a variable the stores a storage address. Just like any other variables you require to first declare a tip variable prior to you can use it. Below is how you have the right to declare a tip variable.

Syntax: data_type *pointer_name;

data_type is the form of the guideline (also recognized as the base type of the pointer). Pointer_name is the surname of the variable, which can be any type of valid C identifier. Let's take part examples:

int *ip way that ip is a tip variable capable of pointing to variables of form int. In various other words, a reminder variable ip have the right to store the deal with of variables of form int only. Similarly, the tip variable fp can only save the deal with of a change of kind float. The kind of change (also known as basic type) ip is a guideline to int and type of fp is a tip to float. A tip variable of form pointer come int have the right to be symbolically stood for as (int *). Similarly, a reminder variable of type pointer come float deserve to be stood for as (float *).

Just like other variables, a guideline is a variable so, the compiler will reserve some space in memory. All tip variable regardless of whether of your base kind will occupy the same room in memory. Normally 4 bytes or 2 bytes (On a 16-bit Compiler) are supplied to keep a reminder variable (this may vary from device to system).

Assigning attend to to pointer Variable #

After proclaiming a guideline variable the next step is come assign some valid memory address to it. You should never usage a pointer variable there is no assigning some valid memory resolve to it, due to the fact that just ~ declaration it contains garbage value and it might be pointing to all over in the memory. The use of an unassigned pointer may provide an unpredictable result. It may even reason the routine to crash.

Here ip is declared as a tip to int, for this reason it have the right to only point to the memory address of one int variable. Similarly, fp can only suggest to the resolve of a float variable. In the last two statements, we have actually assigned the resolve of i and also f come ip and fp respectively. Now, ip points to the variable i and fp point out to change f. It is necessary to note that even if you assign an deal with of a float change to one int pointer, the compiler will certainly not show you any kind of error however you may not obtain the desired result. So together a general rule constantly you should constantly assign the attend to of a variable to its matching pointer change of the same type.


We deserve to initialize the pointer variable at the time of declaration, but in this case, the variable need to be declared and also initialized prior to the pointer variable.

You deserve to assign the value of one guideline variable to one more pointer change If their base kind is same. Because that example:

After the assignment, p1 and also p2 points to the exact same variable marks.


As already said as soon as a pointer variable is claimed it has garbage value and it might be point anywhere in the memory. You deserve to assign a symbolic continuous called NULL (defined in stdio.h) to any kind of pointer variable. The assignment the NULL assures that the guideline doesn't point to any type of valid memory location.

Dereferencing reminder Variable #

Dereferencing a guideline variable simply method accessing data in ~ the attend to stored in the reminder variable. Up until now, we have been using the name of the change to access data within it, yet we have the right to also accessibility variable data indirectly using pointers. To do it happen, we will use a brand-new operator called the indirection operator (*). By placing indirection operator (*) prior to a reminder variable we can access data that the change whose address is save in the tip variable.

Here ip stores deal with of change i, if we place * before ip then us can accessibility data save on computer in the change i. It method the adhering to two statements does the exact same thing.

Indirection operator (*) deserve to be check out as worth at the address. Because that example, *ip can be review as value at resolve ip.

Note: that is advised the you have to never apply indirection operator come an uninitialized guideline variable, doing so may cause unexpected plot or the routine may even crash.

Now we recognize by dereferencing a guideline variable, we can access the value at the address stored in the tip variable. Let's destruction a tiny deeper come view just how the compiler actually retrieves data.

Let's say pointer cp includes the resolve 1000. Once we write *cp the compiler knows that it has to retrieve information from the beginning address 1000. Currently the concern arises how much data to retrieve from beginning address 1000 ? 1 bytes, 2 bytes; What perform you think ? to know how much info to retrieve from beginning address 1000, the compiler looks at the base type of pointer and also will retrieve the information relying on the base type of pointer. For example, if the base type is reminder to char climate 1 byte of details from the starting address will be retrieved and also if the base kind pointer to int climate 4 bytes of details from the beginning address will be retrieved. The is important to note that if you are on a system where the dimension of int is 2 bytes then 2 bytes of details from the beginning address will be retrieved.

So in ours case, only 1 byte the data from beginning address will certainly be retrieved. I.e the data stored at attend to 2000 will certainly be recall only.

Similarly, if ip points come the deal with 2000. On creating *ip compiler will retrieve 4 bytes of data starting from resolve 2000.

In the complying with image, shaded portion shows the number of bytes retrieved.


Before moving ahead, translate the an interpretation of the complying with expression:

*(&i) , whereby i is a change of form int.

We understand from the precedence table that parentheses () has actually the highest possible precedence, so &i is evaluate first. Since &i is the resolve of variable i, for this reason dereferencing it v * operator will provide us the value of the change i. Therefore we deserve to conclude that writing *(&i) is same as writing i.

The following example demonstrates every little thing we have learned about pointers so far.

See more: How Many Calories In A Pound Of Butter (1 Pound (454G)), Calories In Butter, Salted

#includeint main() int ns = 12, *ip = &i; twin d = 2.31, *dp = &d; printf("Value that ip = resolve of i = %d\n", ip); printf("Value the fp = resolve of d = %d\n\n", d); printf("Address the ip = %d\n", &ip); printf("Address that dp = %d\n\n", &dp); printf("Value at attend to stored in ip = worth of ns = %d\n", *ip); printf("Value at resolve stored in dp = worth of d = %f\n\n", *dp); // memory populated by reminder variables // is exact same regardless of its base type printf("Size of tip ip = %d\n", sizeof(ip)); printf("Size of guideline dp = %d\n\n", sizeof(dp)); // signal come operating system program ran good return 0;
Value the ip = deal with of ns = 2686788Value the fp = attend to of d = 1202590843Address of ip = 2686784Address the dp = 2686772Value at address stored in ip = value of i = 12Value at resolve stored in dp = value of d = 2.310000Size of reminder ip = 4Size of guideline dp = 4
Note: Memory address may differ every time you run the program.

There is nothing new in the above program that deserves any explanation. Prior to we proceed to following chapter, always remember that the dimension of the reminder variables is same regardless of its base form but the dimension of the memory deal with that will be accessed while dereferencing depends on upon the base kind of the guideline variable.

Please enable JavaScript to check out the comments powered by Disqus. Report this ad
report this advertisement C Programming indict report this ad