zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    【计算机世界】467- XOR — 神奇的按位运算符
    记 · 复习知识 · 偶遇好玩的知识点
    【CSS】466- 一行 CSS 代码搞定响应式布局
    【Web技术】465- 关于前端埋点统计方案思考
    【CSS】464- 5种 CSS 浮动和清除浮动的方法
    简单易懂的 React useState() Hook 指南(长文建议收藏)
    java中的四类八种
    线程
    异常
    Aspx Ajax 调用 C#函数处理数据
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11208813.html
Copyright © 2011-2022 走看看