Programming C: Posts 12 – The relationship between pointers and arrays, string

1. Pointers and arrays 1 evening.

As in all Arrays we know we can be regarded as a variable array pointer, so we can use the array variable to access the array of pointers way.

//code by nguyenvanquan7826
#include <stdio.h>

void nhapMang(int a[], int n) 
{
    int i;
    for (i = 0; i < n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", &a[i]);
    }
}

void nhapContro(int a[], int n)
{
    int i;
    for (i = 0; i < n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", a + i);
    }
}

void xuatMang(int a[], int n) 
{
    int i;
    for (i = 0; i < n; i++) 
    {
        printf ("%d \t", a[i]);
    }
}

int main() 
{
    // khai bao mang a co n phan tu
    int n = 5;
    int a[n];
    nhapContro(a, n);
    xuatMang(a, n);

    return 0;
}

In the first function was used with normal typing, I will not say much more.

In the second function, we just replace &a by a i for a declaration[20] is a regarded as a pointer and it will allocate memory for one row of cells from a to a + 19. And a + i is the address of a[in] (ie it is equivalent to &the[in]). a pointer to a location[0].

Pointers and arrays in c

Pointers and arrays in C

Also you can declare 1 array then use 1 pointer to the first array, the pointer into the array also.

//code by nguyenvanquan7826
#include <stdio.h>

int main() 
{
    int n = 5, i;
    int a[n], *pa;
    pa = a; // con tro pa tro toi dau mang a

    for (i = 0; i < n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", pa + i);
    }

    for (i = 0; i < n; i++) 
    {
        printf ("%d \t", *(pa + i));
    }

    return 0;
}

Have you noticed: why we do not need to allocate the memory for the pointer pa still use normal, because we have declared a Plate[20] Should the machine has allocated memory cell array to store a, when we perform pointer to array a pointer pa, then the memory cell is already there is no need to allocate more memory cells for pa. Consider the following example:

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    int n = 5, i;

    // cap phat bo nho cho pa
    int *pa = (int *) malloc(n * sizeof(int));

    for (i = 0; i < n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", pa + i);
    }

    for (i = 0; i < n; i++) 
    {
        printf ("%d \t", *(pa + i));
    }

    return 0;
}

In this example we are not in the array declared as a VD ago, so also can not point to any position pa, but want to do is we need to allocate memory cells for such pa.

2. Enter array of functions

The array entry is not always advantageous because as the previous example we need to have the number of array elements before allocated or imported, So if we do not know in advance the number of elements that the user must enter the array function to the stars. You see the following example.

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

void nhapContro(int *(*a), int *n) 
{
    int i;

    printf("Nhap so phan tu cua mang: ");
    scanf("%d", n); // khong phai &n
    *a = (int *) malloc ((*n) * sizeof(int));
    // *a : lay dia chi cua mang a chu khong phai gia tri cua a

    for (i = 0; i < *n; i++) 
    {
        printf("Nhap a[%d] = ", i);
        scanf("%d", (*a + i));
    }
}

void xuatMang(int *a, int n) 
{
    int i;
    for (i = 0; i < n; i++) 
    {
        printf ("%d \t", a[i]);
    }
}

int main() 
{
    int *a, n;

    nhapContro(&a, &n); // lay dia chi cua a va n
    xuatMang(a, n);

    return 0;
}

In this example we performed array of input and output functions, memory allocation functions are also in. The caption stated in its programs and.

Have 1 Point to note that the function by entering a pointer array, there 2 sign *. 1 is a sign of the array (numbered 2), The first sign is also used to transfer addresses the value of the array can be kept out of the jaw, it's like a sign * in function HoanVi(int *the,int *b) so.

3.Pointers and strings

Due to the nature and character strings are arrays of characters so this it is similar to the array 1 evening, I would say over 1 little by 1 simple example.

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    char *name;
    name = (char *) malloc (100*sizeof(char));

    printf("What your name? ");
    gets(name);

    printf("Oh, Hello %s\n", name);

    return 0;
}

4. Pointers and arrays 2 evening, array of pointers - Cursor multilevel

the. Pointers and arrays 2 evening

The upper part we learn about pointers and arrays 1 evening, and this section pointers and arrays 2 way similar to that.

As we know, the essence of computer memory arrays store 2 dimensional array like 1 evening. So we can fully perform array 2 dimensional array of pointers like 1 evening.

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    double a[10][10], *pa;
    int n, m, i;
    pa = (double *) a;
    printf("Nhap so hang va so cot:\n");
    scanf("%d %d", &n, &m);

    for (i = 0 ; i < n * m; i++) 
    {
        printf("Nhap a[%d][%d] = ", i / m, i % m);
        scanf("%lf", pa + i);
    }

    for (i = 0 ; i < n * m; i++) 
    {
        if (i % m == 0) printf("\n"); // xuong dong
        printf("%-5.2lf", *(pa + i));
    }

    return 0;
}

Result:

Enter the number of rows and columns:
2
3
Import a[0][0] = 4.23
Import a[0][1] = 5.7
Import a[0][2] = 1.2
Import a[1][0] = 8.6
Import a[1][1] = 3.456
Import a[1][2] = 12

4.23 5.70 1.20
8.60 3.46 12.00

Moreover, we may not even need to use a pa which is 1 pointer. Or you can enter 1 the same way as arrays 2 normal way as follows.

You note as to why we have However, + in*10 + j. That's because the main function() I declare that a[10][10] Should the machine allocated to us the memory cell array 2 dimensional 10 row, 10 columns that if we do not run out of it still exists.

Pointers and arrays in c

Pointers and arrays 2 way in C

Have you noticed that we can write a[in][j] but can not write pa[in][j] since declared it a plaque 2 afternoon so we could write so there is pa 1 cursor and when we assign pa = a, we have considered a default array 1 evening. So if we are only allowed to write pa[in10+j] (take values) and &However,[in10+j] (Obtain an IP address).

b. Arrays of pointers

Pointer array is an array containing a set of pointers same type.
Float *a[10]; // declare a pointer array. Including 10 pointer: the[0], the[1], …a[9]; was 10 pointer.
Contact Plate 2 way, we have the following comments: Each row of the array 2 pm me as 1 Plate 1 evening. Each pointer, then the relationship with 1 Plate 1 evening, I figured:

Pointers and arrays in c

Pointers and arrays 2 way in C

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    double a[10][10], *pa[10];
    int n, m, i, j;

    printf("Nhap so hang va so cot: ");
    scanf("%d %d", &n, &m);

    for (i = 0 ; i < n; i++) 
    {
        pa[i] = a[i]; // con tro thu i tro den hang thu i
        for (j = 0 ; j < m; j++) 
        {
            printf("Nhap a[%d][%d] = ", i, j);
            scanf("%lf", &pa[i][j]);
        }
    }

    for (i = 0 ; i < n; i++) 
    {
        printf("\n"); // xuong dong
        for (j = 0 ; j < m; j++) 
        {
            printf("%-5.2lf", pa[i][j]);
        }
    }

    return 0;
}

The reason I write in this example is pa[in][j] That is because each is pa 1 pointer to the one-dimensional array. However,[in][j] ie, the jth element of the pointer pa[in].

The above examples we have considered as a declared array[][] should not need to allocate memory for pointers, now want to allocate memory for pointers to arrays 2 Like dimensional array allocated in 1 way, we do the following:

//code by nguyenvanquan7826
#include <stdio.h>
#include <stdlib.h>

int main() 
{
    double **pa;
    int n, m, i, j;

    printf("Nhap so hang va so cot: ");
    scanf("%d %d", &n, &m);

    // cap phat n o nho cho n con tro (n hang)
    pa = (double**) malloc(n * sizeof(double));

    for (i = 0 ; i < n; i++) 
    {
        // cap phat m o nho cho moi con tro (moi hang)
        pa[i] = (double *) malloc(m * sizeof(double));
        for (j = 0 ; j < m; j++) 
        {
            printf("Nhap a[%d][%d] = ", i, j);
            scanf("%lf", &pa[i][j]);
        }
    }

    for (i = 0 ; i < n; i++) 
    {
        printf("\n"); // xuong dong
        for (j = 0 ; j < m; j++) 
        {
            printf("%-5.2lf", pa[i][j]);
        }
    }

    return 0;
}

It can be considered a pointer array, or a pointer to a pointer (multi-level pointers).