指针的运算符

来源:互联网 发布:短信监控软件 编辑:程序博客网 时间:2024/06/05 19:33

指针的运算符

  1. 指针的加法:

    int *p =xxxx;
    p+1 ==》 xxxx+1*(sizeof(*p))
    指针的加法运算,实际上加的是一个单位,单位的大小可以使用sizeof(*p)

  2. 指针的减法:
    指针的减法与加法类似,都是一个单位进行相应的操作。
  3. 自加、自减:
    在这个过程中指针也变换,更新了指针的地址。

  4. []的使用
    变量名[n]: ID名+标签,是一个地址内容的标签访问,类似于数组。可以对指针进行跳跃访问。

  5. 例子:

     #include <stdio.h> int main (void ) {     int a = 0x12345678;     int b = 0x99999119;     int *p1 = &b;     char *p2 = (char *)&b;     printf("the p1+1 is %x,%x,%x\n",*(p1+1)),p1[1],*p1+1);     printf("the p2+1 is %x,%x,%x\n ",p2[1]);     return 0;     }

    以下是一个比较字符串并且排序首字母的程序,同时增加了一个忽略大小写排序的子程序。

    #include <stdio.h>#include <string.h>#include <ctype.h>#define SIZE 81#define LIM 20#define HALT " "void stsrt  (char *string[],int num);void ToUpper(char *str);int main (void ){    char input[LIM][SIZE];    char *ptstr[LIM];    int ct = 0;    int k;    printf("Input up to %d lines,and I will sort them.\n",LIM);    printf("To stop,press the Enter key at a line's start .\n");    while(ct<LIM && gets(input[ct])!=NULL && input[ct][0]!='\0')    {        ptstr[ct] = input [ct];        ct++;    }    stsrt(ptstr,ct);    puts("\n Here's the sorted list: \n");    for(k=0;k<ct;k++)        puts(ptstr[k]);        return 0;}void stsrt (char * string[],int num){    char *temp;    int top,seek;    unsigned int i;    char tem_1[50];    char tem_2[50];    for (top=0;top<num-1;top++)        for(seek=top+1;seek<num;seek++)        {            strcpy(tem_1,string[top]);            strcpy(tem_2,string[seek]);            ToUpper(tem_1);            ToUpper(tem_2);            if (strcmp(tem_1,tem_2)>0)            {                temp = string[top];                string[top] = string[seek];                string[seek]= temp;            }        }    }void ToUpper (char * str){while(*str){    str= toupper (*str);    str++;}

    }

0 0