Introduction to pointers in C

The basic purpose of developing a C programming tutorial for this website – CircuitsToday – is to make it useful for people who wish to work with embedded systems. Really good C programming skill is an essential to work with embedded systems and “Pointers” is the most important concept in C that should be mastered by an embedded systems programmer. “Pointers” are so important because it enables a programmer to work directly with memory of the system. Memory of a system is organized as a sequence of byte sized locations (1 byte = 8 bits). If the total memory of the system is 128 bytes then there will be 128 accessible locations of 1 byte each. Each of these 128 locations are numbered from 0 to 127 in a special manner like 0000, 0001, 0002 …etc. The number associated with a byte is known as the address of the memory location.

You may refer the figure below to get an idea – how memory is organized with in 8051

A pointer is an entity which holds the address of a memory location. So if the address of a location is 2050H, pointer is used to hold this particular address.

Note:- Address of a memory location is always a positive integer. The range of address is from zero to a positive integer constant (which is the address of the last memory location ).

Pointer variables

We can use variables to hold address of a memory location and such variables are known as pointer variables. We have seen before that normal variables are used to store data items of a particular data type (char, int, float etc). Before using a variable in a program, we declare it at the beginning. Similarly we need to declare a pointer variable too in a special way – to let the compiler know we have declared a variable as a pointer (not as a normal variable). To do this we have the *operator – known as indirection operator in C.

Pointer variable declaration

The syntax to declare a pointer variable is 

(data type)   *(variable name);

Ex:-  int  *ptr ;

Here we have declared a pointer variable of name ‘ptr’ and it is of type integer (int). 

Why we need data types in pointers ?

The first doubt that may come to many is, why we need data types to declare a pointer variable. Well, here is the explanation. The address of a memory location will contain a data – rite? And it can be of type char, int, float etc. The difference between all these data types is in the size allocated to each data type. Char – is 1 byte where as int – is 2 byte and float is 4 bytes. Memory is allocated to all these data types as sequential blocks.

Just consider a scenario like this:- 

char a ;

int b;

float c; 

Lets start memory allocation from 2000H. 

Now the character variable ‘a‘ will be allocated 2000H (1 byte), where as integer variable ‘b’ will be allocated 2 bytes using 2001H and 2002H. Finally the float variable ‘c’ will be allocated 4 bytes using 4 consecutive locations – 2003H, 2004H, 2005H, 2006H. Now you might get an idea of why we need data types to declare pointer variables. It is because memories are allocated in sequential blocks according to the type of data holded in those locations.

So when we declare a pointer variable as float *ptr and then assign address of the normal float variable c to ptr – what really happens is – ptr is assigned the sequential block from 2003H to 2006H as a whole. But the variable ptr will hold only the starting address of the sequential block i.e 2003H

So a pointer variable must be declared with a data type. And this data type should be the same data type as of the contents inside the memory location address – which is assigned to the pointer variable. Ex:- If 2000H is assigned to a pointer variable ptr and the contents inside 2000H is a character. In this case the pointer variable ptr should be declared as a character pointer as shown below:-

char  *ptr; 

Note:- In fact we can actually declare a pointer variable without any data type using the keyword void. It is known as a void pointer. The topic of void pointer has to be explained separately – so I will explain it in my next post.

Assigning address to a pointer variable

To make use of a pointer and it’s capabilities – the address of a particular memory location must be assigned to the pointer. It is possible to assign address of single variable or that of an array or the address of a structure etc to a pointer variable. This capability makes pointers the most powerful tool in C programming. We can literally play with the memory of a system using pointers. 

To assign address of an entity to a pointer variable – we use the & operator (address of operator). The operator & fetches the memory location of a variable.

So taking our earlier case as example:-

#include

void main()

{

int *ptr; // Declaring the pointer variable 'ptr' of data type int

int a; // Declaring a normal variable 'a'

ptr=&a; // Assigning address of variable 'a' to pointer variable 'ptr'

}

 

Dereferencing a pointer (getting the real content from a memory location)

We have seen upto assigning an address to a pointer variable. Now how to get the content inside the memory location using the same pointer variable? For that we use the same indirection operator * used to declare a pointer variable.

Consider the scenario:-

#include

void main()

{

int *ptr; // Declaring the pointer variable 'ptr' of data type int

int a=10; // Declaring a normal variable 'a' and assigning value 10 to it.

ptr=&a; // Assigning address of variable 'a' to pointer variable 'ptr'

printf("The value inside pointer is= %d",*ptr); // See the value is printed using *ptr;

}

 

转载于:https://www.cnblogs.com/passedbylove/p/11208813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值