Pointers and Arrays (ii)

Source: Internet
Author: User
Tags arrays definition comparison define function function definition printf relative sort

[Example 6-11] Use the pointer method to input and output two-dimensional array elements.
#include <stdio.h>
Main ()
{
int a[3][4],*ptr;
int i,j;
PTR=A[0];
for (i=0;i<3;i++)
for (j=0;j<4;j++)
scanf ("%d", ptr++);/* Pointer representation method * *
PTR=A[0];
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
printf ("%4d", *ptr++);
printf ("\ n");
}
}
To run the program:
RUN
1 2 3 4 5 6 7 8 9 10 11-12
1 2 3 4

6.4.3 array pointer as a function parameter
After learning to define and correct reference to a two-dimensional array pointer variable, we now learn to use pointer variables as
The parameters of the function.
[Example 6-12] invokes a subroutine that solves the largest element in a one-dimensional array.
Let's begin by assuming that the element labeled 0 in the one-dimensional array is the largest and the pointer variable points to the element. Subsequent elements are associated with the
elements are compared and replaced if a larger element is found. The formal parameter of a subroutine is a one-dimensional array, and the actual parameter refers to
A pointer to a one-dimensional array.
# include <stdio.h>
M a i n ()
{
int Sub_max (); /* Function declaration */
int n,a[10],*ptr=a; /* Define the variable and point the pointer to the array */
int Max;
F o r (n = 0; n < = i-1 N + +)/* Input data */
s c a n f ("% d", & A [n]);
M A x = S U b _ m A x (P t R, 1 0); /* Function call, in fact, the reference is a pointer */
P r i n t F ("m a x =% d \ n", m A x);
}
int Sub_max (b,i)/* function definition, whose formal parameters are array */
int b[],i;
{
int temp,j;
T e m p = b [0];
F o r (j = 1; J < = 9; j + +)
if (Temp<b[j]) temp=b[j];
return temp;
}
The M a i n () function section of the program, defined array A has 1 0 elements, because the first address is passed to P t R, then the pointer
The variable ptr points to the array, invokes the subroutine, and then passes the address to the form parameter B of the subroutine, so that B
The array has the same address in memory as the array A, that is, the memory is completely coincident. The operation of array B in subroutines, and the
Count Group A has the same meaning. The combination of the actual and the false in memory is shown in Figure 6-9.
The M a i n () function completes the input of the data, invokes the subroutine and outputs the running result. The s U B _ m a x () function completes the logarithmic tuple
To find the biggest process. The representation of an array element in a subroutine takes the subscript method. To run the program:
R U N
1 3 5 7 9 2 4 6 8 0
M A x = 9
[Example 6-13] The above program can also use pointer variable as the form parameter of subroutine.
# include <stdio.h>
M a i n ()
{
int Sub_max ();
int n,a[10],*ptr=a;
int Max;
F o r (n = 0; n < = 9; n + +)
s c a n f ("% d", & A [n]);
M A x = S U b _ m A x (P t R, 1 0);
P r i n t F ("m a x =% d \ n", m A x);
}
int Sub_max (b,i)/* form parameter is pointer variable */
int *b,i;
{
int temp,j;
T e m p = b [0]; /* The subscript method of the array element pointer means */
F o r (j = 1; J < = I-1 J + +)
if (Temp<b[j]) temp=b[j];
return temp;
}
In a subroutine, the formal parameter is a pointer, and the actual parameter of the calling program P T R is a pointer to a one-dimensional array a, and the virtual knot
The form parameter B of the subroutine gets the value of P T R and points to the one-dimensional array of memory. The array element is represented by the subscript method, i.e.
The head pointer of a one-dimensional array is B, and the array elements can be represented by B [j]. The combination of the virtual and actual parameters in its memory is shown in Figure 6-1 0.
To run the program:
R U N
1 3 5 7 9 2 4 6 8 0¿
M A x = 9
[Example 6-14] in the subroutine of the above program, the array elements can also be represented by pointers.
# include <stdio.h>
M a i n ()
{
int Sub_max ();
int n,a[10],*ptr=a;
int Max;
F o r (n = 0; n < = 9; n + +)
s c a n f ("% d", & A [n]);
M A x = S U b _ m A x (P t R, 1 0);
P r i n t F ("m a x =% d \ n", m A x);
}
int Sub_max (B,I)/* subroutine definition */
int *b,i;
{
int temp,j;
T e m p = * B + +;
F o r (j = 1; J < = I-1 J + +)
if (temp<*b) temp=*b++;
return temp;
}

In the program, the assignment statement T e m p = * B + +; can be decomposed into: t e m p = * b;b + +; two sentences, first as T e m p = * b;
B + +; The running result of the program is identical with the above.
The above program is modified to not only find the largest element in the subroutine, but also to record the subscript of the element.
# include <stdio.h>
M a i n ()
{
int *max ();/* Function declaration */
int n,a[10],*s,i;
F o r (i = 0; i < 1 0; i + +)/* Input data */
scanf ("%d", a+i);
s = m A x (A, 1 0); /* Function call */
P r i n t F ("m a x =% d, i n d e x =% d \ n", * s, s-a);
}
int *max (A,N)/* Define function to return pointer */
int *a,n;
{
int *p,*t; /* p is used to track the array, T is used to record the address of the maximum element */
F o r (p = A, t = A; p-a < n; p + +)
if (*p>*t) t=p;
return t;
}
In the M A X () function, use P-a < N to control the end of the loop, A is the first address of the array, p is used to track the address of an array element, P-A is exactly the distance of the element being tracked relative to the array header, or the number of elements of the element relative to the array header, so in M a i n () the subscript of the largest element is Is the difference between the address of the element and the array header, that is, the s-a. To run the program:
R U N
1 3 5 7 9 2 4 6 8 0¿
M A x = 9, i n d e x = 4
[Example 6-15] implements a small to large bubble sort for a one-dimensional array with pointer variables pointing to the array. Write three functions for entering data, sorting data, and outputting data.
In the 5th chapter of the example, we introduced the selection method sorting and algorithm, this example again introduced bubble sort algorithm. In order to organize a group of n unordered numbers into a small to large order, put them into a one-dimensional array of [0], a [1] ... a [n-1]. The bubbling algorithm is as follows:
(Open order)
① the adjacent array elements in sequence by 22 comparison, that is, a [0] with a [1] ratio, a [1] and a [2] than ... a [n-2] than a [n-1] ratio, by exchanging to ensure that the adjacent two elements of the array are small, the latter large. This complete 22 comparison will enable a [n-1] to be the largest in the array.
② the remaining n-1 elements, complete 22 comparisons are made according to the above principles, making a [n-2] the largest of the remaining n-1 elements.
③ to complete the total of n-1 22 comparison, so that all the data sorted out orderly.
Here's a sort of process:

4 elements are compared 3 times 22 to get a maximum element. If the adjacent element is represented as a [j] and A [j + 1], the pointer
The variable p points to an array, the adjacent element is represented by the * (P + j) and the * (P + j + 1) program implementation as follows:
# include<stdio.h>
#define N 10
M a i n ()
{
void input (); /* Function declaration */
void sort ();
void output ();
int a[n],*p; /* Define one-dimensional arrays and pointer variables */
i n p u t (a, n); /* Data input function call, argument A is the array name */
p = A; /* The pointer variable points to the first address of the array */
S o r t (p, N); /* Sort, argument p is a pointer variable */
o u T p u t (P, N); /* Output, argument p is a pointer variable */
}
void input (ARR,N)/* input data function definition without return value, parameter a r r is an array */
int arr[],n;
{
int i;
printf ("Input data:\n");
for (i = 0; i < n; i + +)/* Adopt the traditional subscript method * *
s c a n f ("% d", & A R R [i]);
}
void sort (ptr,n)/* bubble sort, parameter ptr is pointer variable */
int *ptr,n;
{
int i,j,t;
for (i = 0; i < n-1 i + +)
for (j = 0; J < n-1-I; j + +)
if (* (PTR+J) >* (ptr+j+1))/phase * Pro Two elements for comparison * *
{
t = * (ptr + j); /* Two elements for Exchange */
* (ptr + j) = * (ptr + j + 1);
* (ptr + j + 1) = t;
}
}
void output (ARR,N)/* Data output */
int arr[],n;
{
int I,*ptr=arr; /* Use the pointer to point to the first address of the array */
printf ("Output data:\n");
for (; Ptr-a R r < N; ptr + +)/* output array n elements */
printf ("% 4 D", * ptr);
printf ("\ n");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.