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=; // 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;

}

Introduction to pointers in C的更多相关文章

  1. Void pointers in C

    In this article we are learning about “void pointers” in C language. Before going further it will be ...

  2. <转>年终盘点!2017年超有价值的Golang文章

    马上就要进入2018年了,作为年终的盘点,本文列出了一些2017年的关于Go编程的一些文章,并加上简短的介绍. 文章排名不分先后, 文章也不一定完全按照日期来排列.我按照文章的大致内容分了类,便于查找 ...

  3. Introduction of OpenCascade Foundation Classes

    Introduction of OpenCascade Foundation Classes Open CASCADE基础类简介 eryar@163.com 一.简介 1. 基础类概述 Foundat ...

  4. Introduction of Open CASCADE Foundation Classes

    Open CASCADE Foundation Classes Open CASCADE基础类 eryar@163.com 一.简介 1. 基础类概述 Foundation Classes Overv ...

  5. Brief introduction to Scala and Breeze for statistical computing

    Brief introduction to Scala and Breeze for statistical computing 时间 2013-12-31 03:17:19  Darren Wilk ...

  6. Introduction the naive“scull” 《linux设备驱动》 学习笔记

    Introduction the naive "scull" 首先.什么是scull? scull (Simple Character Utility for Loading Lo ...

  7. Reading task(Introduction to Algorithms. 2nd)

    Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...

  8. Introduction to Parallel Computing

    Copied From:https://computing.llnl.gov/tutorials/parallel_comp/ Author: Blaise Barney, Lawrence Live ...

  9. [中英对照]Introduction to DPDK: Architecture and Principles | DPDK概论: 体系结构与实现原理

    [中英对照]Introduction to DPDK: Architecture and Principles | DPDK概论: 体系结构与实现原理   Introduction to DPDK: ...

随机推荐

  1. Web前端开发——HTML概述

    HTML  HyperText MakeUp Language,“超文本标记语言”,它是制作网页的标准语言 超文本就是通过链接的方式将文本有机地组织在一起,HTML的标记称为标签. 标签 HTML由标 ...

  2. webpack5持久化缓存

    Opt-in webpack 旨在注重构建安全而非性能.我们没有打算默认启用这一功能,主要原因在于此功能虽然有 95% 几率提升性能,但仍有 5% 的几率中断你的应用程序/工作流/构建. 什么是缓存失 ...

  3. 微信小程序开发入门教程(一)---hello world

    由于无法备案网站,前期做了个微信小程序(开发版)就搁置了,几乎忘了开发过程.现在重新梳理,做个记录. 一.最基本的小程序前端例子hello 1.下载安装  微信开发者工具  官网: https://d ...

  4. js上传超大文件解决方案

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  5. 牛客训练41D最小相似度bfs

    最小相似度 题目大意:对于位数相同的两个二进制串,SIM(A,B)为它们的相似度,也就是A^B中0的个数.现在给定一堆串,找出一个T使得max{SIM(S1,T),SIM(S2,T),......,S ...

  6. 【BZOJ4671】 异或图

    Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中, 否 ...

  7. Codeforces 1214 F G H 补题记录

    翻开以前打的 #583,水平不够场上只过了五题.最近来补一下题,来记录我sb的调试过程. 估计我这个水平现场也过不了,因为前面的题已经zz调了好久-- F:就是给你环上一些点,两两配对求距离最小值. ...

  8. Vue_(组件通讯)父子组件简单关系

    Vue组件 传送门 在Vue的组件内也可以定义组件,这种关系成为父子组件的关系 如果在一个Vue实例中定义了component-a,然后在component-a中定义了component-b,那他们的 ...

  9. 原生Js_实现广告弹窗

    广告样式当页面加载后5s刷新在右下角 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  10. html基础(img、a、列表 )

    图片标签(img) <img src="图片路径" alt="图片描述 图片无法正常显示出现文字" title="爱你"/> i ...