C Tutorial/Array/Array Pointer — различия между версиями
| Admin (обсуждение | вклад) м (1 версия: Импорт контента...) | Admin (обсуждение | вклад)  м (1 версия: Импорт контента...) | 
| (нет различий) | |
Текущая версия на 10:32, 25 мая 2010
Содержание
- 1 Accessing an array using pointers
- 2 Address of second element in array (value of arraypointer+1)
- 3 Arrays and pointers: get address of an array
- 4 Arrays and pointers: get array value through array pointer
- 5 Deal with array pointer of long integer
- 6 Move array pointer to the next element
- 7 The name of an array is the same as &array[ 0 ]
Accessing an array using pointers
The array name is the pointer constant.
   
#include <stdio.h>
main(){
    int a[5];
    int i;
    for(i = 0;i<5;i++){
        a[i]=i;
    }
    int *b;  
   
    b=a;     
    for(i = 0;i<5;i++){
        printf("value in array %d and address is %16lu\n",*b,b);
        b=b+2;
    }
}value in array 0 and address is           631652
      value in array 2 and address is           631660
      value in array 4 and address is           631668
      value in array 631676 and address is           631676
      value in array 42920 and address is           631684
Address of second element in array (value of arraypointer+1)
#include <stdio.h>
int main(void)
{
  char multiple[] = "another string";
  printf("\nAddress of second element: %p", &multiple[1]);
  printf("\nValue of multiple+1      : %p\n", multiple+1);
  return 0;
}Address of second element: 9a372
     Value of multiple+1      : 9a372
Arrays and pointers: get address of an array
#include <stdio.h>
int main(void)
{
  char multiple[] = "My string";
  char *p = &multiple[0];
  printf("\nThe address of the first array element  : %p", p);
  p = multiple;
  printf("\nThe address obtained from the array name: %p\n", p);
  return 0;
}The address of the first array element  : 9a372
     The address obtained from the array name: 9a372
Arrays and pointers: get array value through array pointer
#include <stdio.h>
int main(void)
{
  char multiple[] = "a string";
  char *p = multiple;
  int i;
  for(i = 0 ; i<strlen(multiple) ; i++)
    printf("\nmultiple[%d] = %c  *(p+%d) = %c  &multiple[%d] = %p  p+%d = %p",
                       i, multiple[i], i, *(p+i), i, &multiple[i], i, p+i);
  return 0;
}The address of the first array element  : 9a372
     The address obtained from the array name: 9a372
Deal with array pointer of long integer
#include <stdio.h>
int main(void)
{
  long multiple[] = {15L, 25L, 35L, 45L};
  long * p = multiple;
  int i;
  for(i = 0 ; i<sizeof(multiple)/sizeof(multiple[0]) ; i++)
    printf("\naddress p+%d (&multiple[%d]): %d   *(p+%d) value: %d", i,i, p+i,i,*(p+i));
  printf("\n    Type long occupies: %d bytes\n", sizeof(long));
  return 0;
}address p+0 (&multiple[0]): 631656   *(p+0) value: 15
     address p+1 (&multiple[1]): 631660   *(p+1) value: 25
     address p+2 (&multiple[2]): 631664   *(p+2) value: 35
     address p+3 (&multiple[3]): 631668   *(p+3) value: 45
         Type long occupies: 4 bytes
Move array pointer to the next element
#include <stdio.h>
int main(void)
{
  char multiple[] = "another string";
  printf(" first element: %p\n", multiple);
  printf("second element: %p\n", multiple + 1);
  printf(" third element: %p\n", multiple + 2);
  return 0;
}first element: 9a371
     second element: 9a372
      third element: 9a373
The name of an array is the same as &array[ 0 ]
#include <stdio.h>
int main()
{
   char array[ 5 ]; 
   printf( "    array = %p\n&array[0] = %p\n"
      "   &array = %p\n",
      array, &array[ 0 ], &array );
   return 0; 
}array = 9a37b &array[0] = 9a37b &array = 9a37b
