C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言指针算术

我们可以对指针执行算术运算,例如加法,减法等。但是,由于我们知道指针包含地址,因此算术运算的结果如果另一个操作数是整数类型,则对该指针执行的操作也将是一个指针。在指针到指针的减法中,结果将是一个整数值。使用C语言可以对指针执行以下算术运算:
增量 减量 添加 减法 比较

C语言中的递增指针

如果我们将指针递增1,则指针将开始指向紧邻的下一个位置。这与通用算法有些不同,因为指针的值会随着指针所指向的数据类型的大小而增加。
我们可以遍历数组通过对指针使用增量操作,该指针将不断指向数组的每个元素,对该数组执行一些操作,然后在循环中更新自身。
以下给出了增加指针的规则:
new_address= current_address + i * size_of(data type)
其中i是指针增加的数字。

32位

对于32位int变量,它将递增2个字节。

64位

对于64位int变量,它将增加4个字节。
让我们看看在64位体系结构上递增指针变量的示例。
#include<stdio.h>
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p+1;      
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get incremented by 4 bytes.    
return 0;
}  
输出
Address of p variable is 3214864300 
After increment: Address of p variable is 3214864304 

使用指针遍历数组

#include<stdio.h>
void main ()
{
    int arr[5] = {1, 2, 3, 4, 5};
    int *p = arr;
    int i;
    printf("printing array elements...\n");
    for(i = 0; i< 5; i++)
    {
        printf("%d  ",*(p+i));
    }
}
输出
printing array elements...
1  2  3  4  5

C中的递减指针

像递增一样,我们可以递减指针变量。如果我们递减指针,它将开始指向先前的位置。递减指针的公式如下:
new_address= current_address-i * size_of(data type)

32位

对于32位int变量,它将减少2个字节。

64位

对于64位int变量,它将减少4个字节。
让我们看一下在64位OS上递减指针变量的示例。
#include <stdio.h>          
void main(){          
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p-1;     
printf("After decrement: Address of p variable is %u \n",p); // P will now point to the immidiate previous location.       
}    
输出
Address of p variable is 3214864300 
After decrement: Address of p variable is 3214864296 

C指针添加

我们可以在指针变量中添加一个值。给指针增加值的公式如下:
new_address= current_address + (number * size_of(data type))

32位

对于32位int变量,它将添加2 *数字。

64位

对于64位int变量,它将加4 *。
让我们看一下在64位体系结构上向指针变量添加值的示例。
#include<stdio.h>
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p+3;   //adding 3 to pointer variable  
printf("After adding 3: Address of p variable is %u \n",p);     
return 0;
}  
输出
Address of p variable is 3214864300 
After adding 3: Address of p variable is 3214864312
如您所见,p的地址为3214864300。但是在将3与p变量相加后,它的值为3214864312,即4 * 3 = 12的增量。由于我们使用的是64位体系结构,因此它增加了12、但是,如果我们使用的是32位体系结构,则它仅增加到了6,即2 * 3 = 6、由于整数值在32位操作系统中占用2字节内存。

C指针减法

就像指针加法一样,我们可以从指针变量中减去一个值。从指针中减去任何数字将得到一个地址。从指针变量减去值的公式如下:
new_address= current_address-(number * size_of(data type))

32位

对于32位int变量,它将减去2 *数字。

64位

对于64位int变量,它将减去4 *数字。
让我们看一下在64位体系结构中从指针变量中减去值的示例。
#include<stdio.h>
int main(){
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p-3; //subtracting 3 from pointer variable  
printf("After subtracting 3: Address of p variable is %u \n",p);      
return 0;
}  
输出
Address of p variable is 3214864300 
After subtracting 3: Address of p variable is 3214864288
您可以看到从指针变量中减去3后,它比先前的地址值少12(4 * 3)。
但是,除了减去数字,我们还可以减去一个来自另一个地址(指针)的地址。这将产生一个数字。这不是简单的算术运算,但它遵循以下规则。
如果两个指针属于同一类型,
Address2-Address1 = (Subtraction of two addresses)/size of data type which pointer points
请考虑以下示例,从另一个指针中减去一个指针。
#include<stdio.h>
void main ()
{
    int i = 100; 
    int *p = &i;
    int *temp;
    temp = p; 
    p = p + 3;
    printf("Pointer Subtraction: %d-%d = %d",p, temp, p-temp);
}
输出
Pointer Subtraction: 1030585080-1030585068 = 3

带指针的非法算术

有多种无法对指针执行的操作。由于指针存储地址,因此我们必须忽略可能导致非法地址的操作,例如加法和乘法。下面列出了此类操作。
地址+地址=非法 地址*地址=非法 地址%地址=非法 地址/地址=非法 地址和地址=非法 地址^地址=非法 地址|地址=非法 〜地址=非法

C语言中的函数指针

如上一章所述,指针可以指向C语言中的函数。但是,指针变量的声明必须与功能相同。考虑以下示例,以使指针指向该函数。
#include<stdio.h>
int addition ();
int main ()
{
    int result; 
    int (*ptr)();
    ptr = &addition;
    result = (*ptr)();
    printf("The sum is %d",result);
}
int addition()
{
    int a, b; 
    printf("Enter two numbers?");
    scanf("%d %d",&a,&b);
    return a+b;
}
输出
Enter two numbers?10 15 
The sum is 25  

C语言中函数数组的指针

要理解函数数组的概念,我们必须理解函数数组。基本上,函数数组是一个包含函数地址的数组。换句话说,指向函数数组的指针是指向包含函数指针的数组的指针。请考虑以下示例。
#include<stdio.h>
int show();
int showadd(int);
int (*arr[3])();
int (*(*ptr)[3])();
int main ()
{
    int result1;
    arr[0] = show;
    arr[1] = showadd;
    ptr = &arr;
    result1 = (**ptr)();
    printf("printing the value returned by show : %d",result1);
    (*(*ptr+1))(result1);
}
int show()
{
    int a = 65;
    return a++;
}
int showadd(int b)
{
    printf("\nAdding 90 to the value returned by show: %d",b+90);
}
输出
printing the value returned by show : 65 
Adding 90 to the value returned by show: 155

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4