Home > Back-end >  Consult a pointer array
Consult a pointer array

Time:10-02

Input from the keyboard, a one-dimensional array input to insert which behind the Numbers, Numbers and inserted into the output array after the insertion,
The original array, for example, a [5]=10,12,13,15,11 {}, insert the number 50, inserted into the second behind, output,12,50,13,15,11 {10}
Write down the pointer how

CodePudding user response:

Writing is not necessarily good, but the basic can achieve the requirement, for reference
 
# include & lt; stdio.h>

Int main (void)
{
Int a [5]={0};
Int * p;
Int I, n, d, TMP;
P=(int *) malloc (sizeof (int) * 6);

For (I=0; i<5; I++)
{
The scanf (" % d ", & amp; A [I]);
}
Printf (" please enter a number to insert and to insert behind which number: \ n ");
The scanf (" % d % d ", & amp; N, & amp; d);

For (I=0; i<6; I++)
{
If (I & lt; D)
P=a [I] [I];
Else if (I==d)
[I] p=n;
The else
P=a [I] [I - 1);
}
For (I=0; i<6; I++)
{
Printf (" % d ", p [I]);
}

Free (p);
return 0;

}

CodePudding user response:

If must be inserted into the original array output, then either the original array directly to create a [6], a [2]=50, the remaining unchanged, and then use pointer to int * b=a, output * b++ or b b [0] ~ [6];
Either int * first a=(int *) malloc (sizeof (int) * 5), then a=(int *) realloc (sizeof (int) * 6), and then put a [2] ~ [4] a move back to a [3] - [5], a longer assignment a [2]=50;
And then output a [0] - a, [5].
Don't know how to write you said with a pointer is what meaning,
For one dimensional array, it is not necessary to use a pointer to the array, with a pointer to the first element array int * p=a, then use p [0], [1] or p * (p + 1), * (p + 2) to represent the array elements, or simply a [0], a [1].
If you must use a pointer to the array, then int (* p) [5]=& amp; a; [0], then use (* p) (* p) [1],

CodePudding user response:

This use an array to a spade a spade really trouble, not a little trouble, and the efficiency is low, the code
1. The array a [10 11 12 13 15 | | | |]
2. Insert a, indicating the length increased so I need a b [| | | | | |] six dimensional array, why not directly behind a? For cross-border operation, is the word of do not secure
3 -- -- -- -- -- -- -- -- ok, we'll have a [5] int the unit length of the original array, and a just the current new b [6] int unit length is not as clean as data array
4. Perform an insert operation, for example, we insert behind the number 999 to the first n
5. This means: first n unchanged, 5 - n all backwards, back to the 999 let cok, (the words are spoken here, a little think should have the ability to solve the)
  • Related