Pointers in C
Pointers concepts in C Language
A pointer is a derived data type in ‘c’ which is constructed from fundamental data type of ‘c’ language.
- A pointer is variable which holds address of another variable.
- By using pointer, we can access the data, which is available outside the f’n.
- By using pointer , we can handle the data structure more effectively.
- Pointers can increase the execution speed of the program.
- When we are working with the pointers, we require to use following operators .i.e
- Address of&
- int direction operator * (or) reference operator (or) object at location (or) value at address.
- Always address of (&) operators returns base address of a variable i.e. starting cell of a variable.
- Indirection operator always returns value of address.
- When we are working with the pointers we require to use following operations.
- *(address of)
- *( In direction operator (or) dereference operator (or) object at location (or) value at address.
- Always address of operator returns base address of a variable i.e. starting cell of a variable.
- Indirection operator always returns value of address.
Datatyep * ptr;
int * ptr;
- It is a variable of type an integer it is a value type variable which holds an integer value.
- ptr is a variable of type int*,it is address type which holds an integer address.
pf(“\n %d %u”,&a,ptr);
pf(“\n %d %d “, a * ptr);
pf(“\n %p %p”, &a ptr);
Note: For ‘c’ programming language the standardized complier is Tc-30
- In above program ptr will holds (address ofa) &a that’s why ptr and &a data both are same.
- ptr is holding address of &a that why *ptr &a values are same.
- Tc-3.0 designed on 8086 architecture i.e. dos 16 bit operating system is aplatform
- On Tc – 3.0 whenever a ‘C’ application is designing then it occupies one 1MB data only.
- This completes 1MB data divided in to 16 equivalent parts called segment.
- Each and every segment having a unique identification value called segment address which starts from OXO and end with OXF.
- Among those all segments 9th segment i.e. Oxd is called data segment, OXA is called graphics video segment & OXB is called tex video segment.
- Whenever we are declaring any variable that variable will occupies the memory in data segment.
- Every segment capacity is 64kb only e 65536 bytes
- This complete 64kb data will divided into small partitions is called cells.
- Each cell capacity is 1 byte only and each and every cell having a unique identification value called offset-address which starts from OXOOOO and ends with OXFFFF.
- In implementation when we require to print the address of the variable, then we require to use %x,%p&%Lp will prints the address in Hexadecimal format ,%u ,%lu prints in decimal format.
- %x ,%p&%u will prints 32 bit physical address.
For printing the address of a variable we can’t use %d format specifier because
- Physcial addresses are there in the form of hexadecimal type from the range of OXOOOO to OXFFFF in decimal 0to65535 so this range will not supported by %d (-32768 to 32767)
- There is no any negative representation are available in physical address but %d will print –ve data also.
printf(“\n %x”, &i ,ptr);
printf(“\n %d %d “, i,*ptr);
printf(“\n %p %p,&i, ptr);
printf(“\n %d %d,i,* ptr);
prinft(“\n%d %d %d %d “ ,a,b,*ptr);
printf(“\n %d %d %d %d “,a,b,*ptr);
O/p: 11 21 11
31 41 41
- In implementation ,when we require to change ptr direction from one variable to another variable rearranging the address to the ptr is possible to chnge.
- Different types of variables are containing different types of sizes because internal content is different.
- Any type of pointer size is same only because internal content is address that is common for any data type.
- On Dos operating system, at runtime addresses are limited because, it doesn’t having memory management.
- On Dos based complier (Tc-3.0.8086 based, 16 bit complier) the size of the pointer is 2 bytes only because addresses are limited from the range of 0 to 65535.
- Windows operating system contains proper memory management that why at the time of execution, millions of address will be created.
- On windows based complier (TC-4.5, Unix/linux,32 bit compliers) the size of the pointer is 4 bytes because addresses are unlimited.
- Any type of pointer size is”2” bytes only because it holds the common data OXOOO to OXFFFF.
- Any type of pointer will holds base address of a variable e single cell information but at the time of applying the in-direction operator, it dereference to pointer type ,then depends on the pointer type from even base address, ”n” no. of bytes will be access.
- In c programming language any type of pointer can hold any type of variable address but we can’t access behavior is data type dependent.
- In implementation, when we are working with an int variable ,then go for int*,float variable float*.and character variable is char* only because we can see the difference at the of applying in direction operator.
ptr=&a; - address
b=*ptr; - accessing
printf(“\n %d % d %d “,a,b,*ptr);
printf (“\n %d %d %d “,a, b,*ptr);
O/p: 32767 -1 -1
32512 -1 0
- On integer variable when we are applying character pointer then it can access and manipulate only 1 byte data.
- When we are working with the pointers, generally we are getting following warning messages with the pointer i.e. suspicious pointer conversion. This warning message we will get when we are assigning address of a variable into different type of pointer.
- Suspicious pointer converters are not allowed in C++.
- Non portable pointer conversion.
- This warning message we will get, when we are assigning value type data to a pointer variable.