Programming C: Posts 12 – The relationship between pointers and arrays, string
Content
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].
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] = 124.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.
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:
//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).
he oi.cho i / m with i% m z is why he…. I do not understand this place
what i / m is the current pickup, in % m is obtained current column. VD array a[10][10] when i = 21, current row 2, current column 1. Because arrays 2 pm is considered as the rows lined up 1 row as array 1 acquiesced to you.
Oh there is a more simple opp c ++ or not…e learning with…find the C language only see his shuttle
Currently only C whiff.
A bit dear for e asked: In the example array entered by using a function pointer level 2 with annotations arrays can remain valid unless there ham.E out there but try to rewrite the function parameter is a pointer to the level 1, the result is an array still get value for a possible explanation is e not?Incidentally when asked a pointer need f?
Here 's e code:
#include
#include
#include
void nhapmang(int *a,int n)
{
for(int i=0;in<n;i )
{
printf("\na[%d]=",in);
scanf("% D",(a+i));
}
}
void in(int *a,int n)
{
for(int i=0;in<n;i )
{
printf("%d ",the[in]);
}
}
int main()
{
int *a;
int n;
a=(int*)malloc(n*sizeof(int));
printf("\nNhap n=");
scanf("% D",&n);
nhapmang(the,n);
in(the,n);
}
Sorry you are not on the blog a few days. Array retains pointer value, of course, keep you ah. Since cursor is a form of array, it's nothing more flexible.
F pointer used when you need to manipulate the array f. Because the array into the function f as required to define the number of rows of columns, Pointers are not required.
His article meticulously, interesting and useful. Thank you for posting. Hope to have many more articles are posted! God.
Part dynamic memory allocation for pointers without me huh. Hi vongj he further complained earlier this.
You look at some 9 nhé.
a oi, e do not understand place “*(*the)” What is this sir?
void nhapContro(int *(*the), int * n)
Oil * The first is to be able to keep the value they get out of function. Oil * letter 2 refers to a pointer
Waiter, launched at the segment level 2 dimensional why I used this program does not run like this DC.
*pa = (double*) malloc(n * sizeof(double));
for (i = 0 ; in < n; i ) {
// phat m o grape cap for each pointer (moi hang)
However,[in] = (double *) malloc(m * sizeof(double));
The first command you need to use this as offline
pa = (double**) malloc(n * sizeof(double));
how to give pointers on the end of a string ak
for (i = 0; in < *n; i ) {
printf("Nhap a[%d] = ", in);
scanf("% D", (*the + in));
}
}
// *n with (*a+i) here I still do not get it, he went through to help you get hk, thanks you so much more :)))
Because n is passed pointers, should like to take its value to users * n, * n mean that measuring the value of n. Should a longer array can be used as pointers. *the + i is the ith position in the array a.
Oh you the size of the array is 1 constants are not usually dc transformer.
int main()
{
const int n=5;
int a[n];
nhapContro(the, n);
xuatMang(the, n);
getch();
return 0;
}
I asked him for them to use the array pointer to save names and save them to the buffer are separated from 1 array names, the running program is stopped. do not know where the error he helped me see
void TachTen(char * s, char *a){
int l, in, j, to;
i = strlen(with)-1;
j = 0; k = 0;
l = strlen(with);
fflush(stdin);
while(with[in] != ‘ '){
in–;
}
while(in < the){
the[j] = s[i 1];
i ;
j ;
}
}
void TachHo(char * s, char *b){
int i, j, to;
i = strlen(with)-1;
j = k = 0;
fflush(stdin);
while(with[in] != ' '){
in–;
}
for(j=0; j<in; j ){
b[j] = s[j];
}
}
case 3: //sx under ten
printf("\nn = %d", n);
for(i=0; in<n; i ){
the[in] = (char *)malloc(100*sizeof(tank));
b[in] = (char *)malloc(100*sizeof(tank));
}
for(i=0; in<n; i ){
TachTen(SV[in].Ten, the[in]);
printf("\nTen sv[%d]: ", i 1);
for(j=0; j<strlen(the[in]); j ){
printf("%c", the[in][j]);
}
TachHo(SV[in].Ten, b[in]);
printf("\nHo sv[%d]: ", i 1);
for(j=0; j<strlen(b[in]); j ){
printf("%c", b[in][j]);
}
}
Co the ban bi loi o lenh while(with[in] != ‘ ‘){
i–;
}
in– Please have some.
yourself questions at the pointer and array 2 dimensional why this code
pa = (double *) the;
why he had to add (double *) the; ?.
It is allowed to cast a pointer variable of type double. Since pa pointer variable should cast such.
themselves asked to exercise it when assigning array pointer pa = a and pa when assigning exam = (double *) a so. I would like to thank !!
Ah with all the array is not required where the cursor, you just do normal ok. This article outlined his theory about the relationship between them alone.
darling place i * 10 + pa + j he can explain a closer is not it.
pa is a pointer. However, + x is more advanced pointer x unit. It is equivalent to a[x]. However, + in 10 * + j equivalent to a[i*10+j].
Oh for f a question in the first example 3 part 1, Why, when e pa cấ memory for Kids show 3 or 2 memory cells(n) DC still enter enough 5 Molecular sir:
pa = (int *) malloc(3 * sizeof(int));
He asked me in the void(int *(*the), int * n)
no need to allocate memory for DC n still enter his game ?
Because when this function calls used &n to transmit the address to be without nhé.
Your ad using arrays pointers to import and export the characters but their swim was repeatedly passed over milk, ad view their fears with:
#include
#include
void nhap(char** s, int n){
for(int i=0;in<n;i ){
printf("\ns[%d]=",in);
scanf("%s",with[in]);
}
printf("\ns[%d]=%s",0,*with[0]);
}
void xuat(char** s, int n){
for(int i=0;in<n;i ){
printf("\ns[%d]=%s",in,*with[in]);
}
}
int main(){
int n;
printf("nhap n:");
scanf("% D",&n);
char * s[n];
nhap(with,n);
printf("\nMang vua nhap: ");
xuat(with,n);
}
you can use a pointer always array 2 afternoon but results k star-like figure out that he entered ?
#include
int main()
{
int a[10][10];
int n,m,in;
printf(“Enter the number of rows and columns : “);
scanf(“%d %d”,&n,&m);
for(i = 0;in < n*m;i )
{
printf("a[%d][%d] :",i/m,i%m);
scanf("% D",a+i);
}
for(i = 0;in<n*m;i )
{
if(in % m == 0) printf("\n");
printf("%d ",*(a+i));
}
return 0;
}
His article is very useful.