Learn C programming in 11 days

Day 7 Pointers

Simple pointer

 

/*********************************************************/

/*  pointer1.cpp - VC6.0                                 */

/*  Simple pointer                                       */

/*  J Luo, 18-Mar-2001                                   */

/*********************************************************/

 

#include<stdio.h>

 

void main()

{

     int a;

    int *p;

 

    printf("a=");

    scanf("%d", &a);

    printf("a=%d\n", a);

 

    p = &a;

    printf("*p=%d\n", *p);

    printf("p=%x\n", p);

    printf("&p=%x\n", &p);

 

    p = p + 1;

    printf("p=%x\n", ++p);

}

 

String with array

 

/*********************************************************/

/*  strings.cpp - VC5.0                                  */

/*  Using array and pointer for string                   */

/*  J Luo, 23-May-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <string.h>

 

void main()

{

    char a[][4] = {"CAT", "RAT", "DOG", "PIG", "FOX", "COW"};

 

    int n = sizeof(a) / sizeof(a[0]);

 

    for (int i=0; i<n; i++) {

         for (int j=0; j<4; j++) {

             printf("%x ", &a[i][j]);

             printf("%c\t", a[i][j]);

         }

         printf("\n");

    }

}

 

 

Output

 

12ff68 C    12ff69 A    12ff6a T    12ff6b    

12ff6c R    12ff6d A    12ff6e T    12ff6f    

12ff70 D    12ff71 O    12ff72 G    12ff73    

12ff74 P    12ff75 I    12ff76 G    12ff77    

12ff78 F    12ff79 O    12ff7a X    12ff7b    

12ff7c C    12ff7d O    12ff7e W    12ff7f    

 

3D array

 

/*********************************************************/

/*  strings.cpp - VC5.0                                  */

/*  Using array and pointer for string                   */

/*  J Luo, 23-May-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <string.h>

 

void main()

{

    char a[][3][4] = {"CAT", "RAT", "DOG", "PIG", "FOX", "COW"};

 

    int n = sizeof(a) / sizeof(a[0]);

 

    for (int i=0; i<n; i++) {

         for (int j=0; j<3; j++) {

             for (int k=0; k<4; k++) {

                  printf("%x ", &a[i][j][k]);

                  printf("%c\t", a[i][j][k]);

             }

             printf("\n");

         }

         printf("\n");

    }

}

 

Output

 

12ff68 C 12ff69 A 12ff6a T 12ff6b

12ff6c R 12ff6d A 12ff6e T 12ff6f

12ff70 D 12ff71 O 12ff72 G 12ff73

 

12ff74 P 12ff75 I 12ff76 G 12ff77

12ff78 F 12ff79 O 12ff7a X 12ff7b

12ff7c C 12ff7d O 12ff7e W 12ff7f

 

 

2D array with pointer

 

/*********************************************************/

/*  strings.cpp - VC5.0                                  */

/*  Using array and pointer for string                   */

/*  J Luo, 23-May-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <string.h>

 

void main()

{

    char *a[][4] = { {"CAT", "RAT", "DOG"}, {"PIG", "FOX", "COW"} };

 

    int n = sizeof(a) / sizeof(a[0]);

 

    for (int i=0; i<n; i++) {

         for (int j=0; j<3; j++) {

             for (int k=0; k<4; k++) {

                  printf("%x ", &a[i][j][k]);

                  printf("%c\t", a[i][j][k]);

             }

             printf("\n");

         }

         printf("\n");

    }

}

 

2D array with pointer

 

/*********************************************************/

/*  strings.cpp - VC5.0                                  */

/*  Using array and pointer for string                   */

/*  J Luo, 23-May-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <string.h>

 

void main()

{

    char *a[][4] = { {"CAT", "RAT", "DOG"}, {"PIG", "FOX", "COW"} };

 

    int n = sizeof(a) / sizeof(a[0]);

 

    for (int i=0; i<n; i++) {

         for (int j=0; j<3; j++) {

             printf("%x ", a[i][j]);

             printf("%s\t", a[i][j]);

         }

         printf("\n");

    }

}