Memory_Region_Access

Pointers in C

A pointer points to an address of same type.

Yes, You read it right, Pointer points to an address of the same type that means if it is an int type it will point to int value similarly if it is a char type it will point to char value. This applied to pre-defined data types or user-defined data types.

Advertisements
//C/C++
#incude<iostream>
using namespace std;
int main(){
int a = 10;             // integer variable
int * int_ptr =  &a;    // integer pointer points to integer type
char ch = 'A';          // Char variable
char * char_ptr = &ch;  // char pointer points to character type
float fl = 0.5;         // float variable
float * flt_ptr = &fl;  // float pointer points to float type
return 0;
}

One thing you can notice is all pointer points to their type If you try to point to a different kind then you will get an error.

char_ptr = &a;
// error: cannot convert ‘int*’ to ‘char*’ in assignment
// char_ptr = &a;

Now the main question is how they work inside memory …

Memory-Address-and-Area
Memory-Address-and-Area
Advertisements

Each unit of memory has address ranging from 0x0000 0000 to 0xffff ffff [depends] . Each unit is used to store information and to get that information we need unit’s address . Basically using that address we perform memory read/write operation. Suppose if you have one Gb file stored at particular location ,instead of coping whole data and passing to sub program ,we can definitely say that take this memory address and go there and do whatever you want to do within 1Gb area and this is one way how pointer reduce space complexity, and execution time faster.

Overall you can say, Pointer points to an address, and using this address we can get data of its own type. In another way, by knowing the length of data you can do read/write operation in that memory region.

Advertisements

3 comments

Leave a comment

Advertisements

Advertisements

3 comments

Leave a comment