Pointers and Arrays (iii)

Source: Internet
Author: User
Tags arrays expression include printf

Because the function call of C program is called by value, that is, when the actual parameter is combined with the formal parameter, the real arguments value is passed to the formal parameter, so when we use the function to process the array, if we need to modify the array in the subroutine, we can only pass the addresses of the arrays and call the addresses. Data is modified in the same address range as memory.

In practical applications, if a subroutine is needed to handle the array, the function invocation uses pointers to arrays (one-dimensional or multidimensional) as arguments, whether the argument or the formal parameter has the following four cases:

We know that two-dimensional arrays are stored by rows in memory, assuming we define two-dimensional arrays and pointers as follows:
int a[3][4],* p = a [0];
The pointer p points to a two-dimensional array. It is stored in memory as shown in Figure 6-11.

From the above storage situation, if you pass the first address of the two-dimensional array to the pointer p, the mapping process is shown in Figure 6-11
as shown. All we have to do is to find the largest element and subscript in the one-dimensional array represented by P, which can be converted into a two-dimensional array of
Number of rows.
# include<stdio.h>
M a i n ()
{
int A[3][4],*PTR,I,J,MAX,MAXI,MAXJ;
/* m A x is the largest of the array, m A x i is the largest element of the row, m A x j is the largest element of the column */
F o r (i = 0; i < 3; i + +)
F o r (j = 0; J < 4; J + +)
s c a n f ("% d", & A [i] [j]);
P t r = a [0]; /* Pass the first address of the two-dimensional array to the pointer variable */
M A x _ a r R (P t r, & M A x, & M A x i, 1 2);
M A x j = m A x i% 4; /* There are four elements per line, please find the column */
m A x i = m a x I/4; /* Find the element's Row */
printf ("max=%d,maxi=%d,maxj=%d", MAX,MAXI,MAXJ);
}
int Max_arr (B,P1,P2,N)
int *b,*p1,*p2,n;
/* b A pointer to a two-dimensional array, p 1 points to the maximum, and P 2 points to the position of the maximum in the one-dimensional array, */
/* n is the size of the array */
{
int i;
*P1=B[0]; *p1=0;
F o r (i = 1; i < n; i + +)/* Find Max */
if (B[I]>*P1) {*p1=b[i]; *p2=i;}
}
To run the program:

6.4.4 Pointer and character array
In the previous lesson, we used the character array, which is the array name to represent the string, the array name is the first address of the array, is the starting address of the string. The following example is used for the input and output of a simple string.
#include <stdio.h>
Main ()
{
Char str[20];
Gets (str);
printf ("%s\n", str);
}

Now, we assign the name of the character array to a pointer variable that points to the character type, so that the character type pointer points to the word
String is the first address in memory, and the representation of strings can be implemented with pointers. The method is defined as: charstr[20],
*p=str; Thus, string Str can be represented by the pointer variable P.
#include <stdio.h>
Main ()
{
Char str[20],*p=str;/*p=str means that the first address of the character array is passed to the pointer variable p*/
Gets (str);
printf ("%s\n", p);
}
RUN
Good morning!
goodmorning!
It should be explained that the character array is different from the string, the string is a special form of the character array, stored with "" "End, so the character array that holds the string should be 1 larger than the string. For character arrays that hold characters, the output can only be entered as characters by character if the "No" end flag is added.
The correct way to use the [example 6-18] character array.
#include <stdio.h>
Main ()
{
CHARSTR[10],*P=STR;
int i;
scanf ("%s", str);/* The string entered is longer than 10*/
for (i=0;i<10;i++)
printf ("%c", *p++);/* Correct output */
printf ("\ n");
P=STR;
printf ("%s", p);///* character array has no ' I ' flag, output error * *
Puts (str);///* character array without ' the ' mark ' flag, output error * *
}
The character array in the above program is output as a string, and if there is no "no" flag, the end flag cannot be found and the output
Wrong.
[Example 6-19] handles replication of two strings with a pointer variable that points to a string.
Copy of string Note: If you copy string 1 to String 2, make sure that the string 2 is longer than or equal to string 1.
#include <stdio.h>
Main ()
{
Char str1[30],str2[20],*ptr1=str1,*ptr2=str2;
printf ("INPUTSTR1:");
Gets (STR1);/* Input str1*/
printf ("INPUTSTR2:");
Gets (STR2);/* Input str2*/
printf ("str1------------str2\n");
printf ("%s.......%s\n", PTR1,PTR2);
while (*PTR2) *ptr1++=*ptr2++;/* string Copy * *
*ptr1= ';///write the end sign of the string * *
printf ("str1------------str2\n");
printf ("%s.......%s\n", STR1,STR2);
}
In the description section of the program, the defined character pointer points to the string. The statement while (*PTR2) *ptr1++=*ptr2++ first tests the value of the expression, and executes the loop *ptr1++=*ptr2++ if the pointer points to a "yes", the ASCII value of the character is 0, the value of the expression is false, the loop ends, and the value of the expression is Non-zero. The statement *ptr1++ according to the operation priority level, first calculates *PTR1, then calculates ptr1++.

Now, we modify the statement in the program printf ("%s.......%s\n", STR1,STR2) for printf ("%s.......%s\n", PTR1,PTR2);
What will happen? Please think.
[Example 6-20] handles a merge of two strings with a pointer variable that points to a string.
#include <stdio.h>
Main ()
{
Char str1[50],str2[20],*ptr1=str1,*ptr2=str2;
printf ("INPUTSTR1:");
Gets (STR1);
printf ("INPUTSTR2:");
Gets (STR2);
printf ("str1------------str2\n");
printf ("%s.......%s\n", PTR1,PTR2);
while (*PTR1) ptr1++;/* moves the pointer to the end of the string * *
while (*PTR2) *ptr1++=*ptr2++ string//* Connection * *
*ptr1= ';///write the end sign of the string * *
PTR1=STR1;PTR2=STR2;
printf ("str1------------------str2\n");
printf ("%s.......%s\n", PTR1,PTR2);
}

It should be noted that, string replication, the length of the string 1 should be greater than or equal to the string 2, when the string connection, the string 1 length should be greater than the sum of string 1 and the length of the string 2.

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.