C pointer (1) - Application of pointer in array (program explanation)

Keywords: C++

2-1. Definition of C array pointer:

#include <stdio.h>
int main()
{
     char str[]="China Beijing Fujian";  //Define and initialize array str
     char *pstr;                         //Define pointer variable pstr
     pstr=&str[6];                       //Initialize pstr and make the pointer variable pstr point to array str
     printf("str:%s,str");
     printf("pstr point str[6]:%c\n,*pstr");
     return 0;
}

Results:

str:China Beijing Fujian
pstr point str[6]:B

2-2.c array pointer reference

#include <stdio.h>
int main(void)
{
  char str[]="China Beijing Fujian";  //Define and initialize array str
  char *pstr;                         //Define pointer variable pstr
  printf("str:%s\n",str);             //Output array str,% s represents string
  pstr=str;                           //Initialization pointer variable pstr, pstr points to array str; pstr and STR point to array STR first address
  printf("pstr->str:%s\n",pstr);      //Output array elements pointed by pstr as% s
  printf("&str=%p\n",&str);           //%The value of the p pointer, which outputs the address of the array name STR, that is & str. The first address of a contiguous array space, that is, the address of str[0]
  printf("pstr=%p\n",pstr);           //Outputs the value of the pointer variable pstr. Because pstr points to array str, the value of pstr is the address of str, and also the address of array element str[0]
  printf("&str[0]=%p\n",&str[0]);
  return 0;
}

Results:

str:China Beijing Fujian
pstr->str:China Beijing Fujian
&str=0x7fffd12e1c80
pstr=0x7fffd12e1c80

 2-3

#include <stdio.h>
int main(void)
{
  char str[]="China Fujian";
  char *pstr;
  pstr=str;
   while(*pstr)
   {
     printf("%c:%p\n",*pstr,pstr);    //%c single character, when executed for the first time, pstr points to str[0], and outputs str[0] and & str[0]
     pstr++;                          //Let the pointer point to the next array element
   }
  return 0;
}

Results:

C:0x7fff13c1f1b0
h:0x7fff13c1f1b1
i:0x7fff13c1f1b2
n:0x7fff13c1f1b3
a:0x7fff13c1f1b4
 :0x7fff13c1f1b5
F:0x7fff13c1f1b6
u:0x7fff13c1f1b7
j:0x7fff13c1f1b8
i:0x7fff13c1f1b9
a:0x7fff13c1f1ba
n:0x7fff13c1f1bb

Subscript representation of 2-4.c array

#include <stdio.h>
#define WEEKNUM 7 / / define a macro
int main(void)
{
  int temp;
  int week[WEEKNUM]={1,2,3,4,5,6,7};
  printf("please input today is:");
  scanf("%d",&temp);
  if(temp<=WEEKNUM)
  {
    printf("tomorrow is :%d\n",week[temp]);  //%d decimal signed character
  }
  else
  {
    printf("Error \n");
   }
   return 0;
}

Results:

please input today is:4
tomorrow i

 2-5

#include <stdio.h>
#define LEN 8               
int main(void)
{
   char i;
   char str[LEN]="Fujian";
   printf("str:%s\n",str);
   for(i=0;i<LEN;i++)
   {
     printf("%c:%p\n",str[i],&str[i]);
   }
   return 0;
}

Results:

str:Fujian
F:0x7fff3f5c5d20
u:0x7fff3f5c5d21
j:0x7fff3f5c5d22
i:0x7fff3f5c5d23
a:0x7fff3f5c5d24
n:0x7fff3f5c5d25
:0x7fff3f5c5d26
:0x7fff3f5c5d27

Pointer representation of 2-6.c array

#include <stdio.h>
#define LEN 10
int main(void)
{
  char str[LEN]={'A','B','C','D','E','F','G','H','I','J'};
  char idx,*pstr;
  pstr=str;
  printf("please input (0-9)and ENTER:\n");
  scanf("%d",&idx);
  if(idx<LEN)
  {
   printf("The character is:%c\n",*(pstr+idx));    //When running pstr+idx operation, the pointer pstr points to str[idx],*(patr+idx) is the array element str[idx]
  }                                                 
  else
  {
    printf("The idx is overflow \n");
   }
  return 0;

Results:

please input (0-9)and ENTER:
4
The character is:E

Subscript method and pointer of 2-8.c array

#include <stdio.h>
#define LEN 15
int main(void)
{
   char str[LEN]="Fujian.2018";
   char idx,*pstr;
   for(idx=0;idx<LEN;idx++)
   {
     printf("%c",str[idx]);                    //subscripts
    }
     printf("\n");
     pstr=str;
     while(*pstr)
    {
      printf("%c",*(pstr+idx));               //Pointer method
      pstr++;
     }
     printf("\n");
     return 0;
}

Results:

Fujian.2018

2-9.c reference to pointer array

#include <stdio.h>
int main(void)
{
  char *str[]={"Fujian","Huian"};      //Define array str and initialize
  printf("str[0]:%s\n",str[0]);        //%s output a string at a time. To output a string at a time, you need to know the first address of the string. str[0] is the first address
  printf("str[1]:%s\n",str[1]);
  return 0;
}

Results:

str[0]:Fujian
str[1]:Huian

2-10

#include<stdio.h>
int main()
{
  char temp;
  char *str[]={"IllgalDay","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
  printf("please input number:\n");
  scanf("%d",&temp);
  if(temp<7)
 {
    switch(temp)
     {
      case 0:
       printf("Ingall day\n");
       break;
      case 1:
       printf("Monday\n");
      break;
      case 2:
       printf("Tuesday\n");
      break;
     }
  }
  return 0;
}

Results:

please input number:
1
Monday

2-11.c string pointer reference

#include <stdio.h>
int main(void)
{
  char *str="Fujian 2018";  //Equivalent to char * str; STR = "Fujian 2018";
  printf("%s\n",str);   //The pointer variable str points to the first address of "Fujian 2018" of the string. Here it just points to whether to assign or not
  return 0;
}

Results:

Fujian 2018

2-12 array implementation

#include <stdio.h>
int main(void)
{ 
  char str[]="Fujian 2018";  //Define the array and initialize it
  printf("%s\n",str);   
  return 0;
}

Results:

Fujian 2018

To define a string in array form, you need to know the size of the string, while pointer form has no such limitation

2-13  

#include <stdio.h>
int main()
{
  char *str="Fujian 2018";
  printf("%s\n",str);     //%s string
  printf("%p\n",str);     //%p pointer, output str, string first address
  while(*str)
   {
    printf("%c",*str);    //Output character
    printf("  %p\n",str); //Output character address
    str++;
   }
    return 0;
}

Results:

Fujian 2018
0x4006c8
F  0x4006c8
u  0x4006c9
j  0x4006ca
i  0x4006cb
a  0x4006cc
n  0x4006cd
   0x4006ce
2  0x4006cf
0  0x4006d0
1  0x4006d1
8  0x4006d2

Step by step

Posted by skylark2 on Fri, 31 Jan 2020 00:23:43 -0800