• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Pointers in C

Pointers concepts in C Language

Pointers:

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.

 

Advantages:

  • 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
  1. Address of&
  2. 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.

 

  1. *(address of)
  2. *( 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.

  

 

Syntax:

Datatyep * ptr;

void main()

{

int i;

int * ptr;

ptr=&1;

}

  • 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.

 

void main()

{

int a;

int*ptr;

ptr=&a;

a=10;

pf(“\n %d %u”,&a,ptr);

pf(“\n %d %d “, a * ptr);

*ptr=20;

pf(“\n %p %p”, &a ptr);

 

Note:  For ‘c’ programming language the standardized complier is Tc-30

 

10

 

11

 

  • 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.

Note:

For printing the address of a variable we can’t use %d format specifier because

  1. 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)
  2. There is no any negative representation are available in physical address but %d will print –ve data also.

 

 

void main()

{

int i;

int *ptr;

ptr=&I;

i=21;

printf(“\n %x”, &i ,ptr);

printf(“\n %d %d “, i,*ptr);

*ptr=11;

printf(“\n %p %p,&i, ptr);

printf(“\n %d %d,i,* ptr);

}

65534

12

 

 

 

void main()

{

int a,b;

int *ptr;

ptr=&a;

a=11;

b=21;

prinft(“\n%d  %d %d %d “ ,a,b,*ptr);

*ptr=31;

ptr=&b;

*ptr=41;

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.

 

 

13

 

 

  • 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.

 

  • Void main()

{

int a,b;

char*ptr;

ptr=&a;  – address

a=32767

b=*ptr;  – accessing

printf(“\n %d % d %d “,a,b,*ptr);

*ptr=0;

printf (“\n %d %d %d “,a, b,*ptr);

}

O/p: 32767   -1      -1

32512    -1      0

14

 

 

  • 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.

 

 

  • void main()

{

int a=10;

int*ptr;

ptr=a;

}

 

 

 

 

Summary
Review Date
Reviewed Item
Pointers in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Pointers in C"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.