Home > Back-end >  Joseph problem
Joseph problem

Time:10-02

Teacher speak of a problem today, array implementation issues, Joseph
I still don't know how to understand the name of an array is passed to a int [] b

CodePudding user response:

Argument is the address of a, parameter can literally written, a [], [] can b

CodePudding user response:

The compiler is actually use Pointers instead of array name parameter
Array name a, itself represents an address first address (array), can be a pointer variable assignment to others, (this is the right value)
But you can't give his assignment
For example,
Int a, [10].
Int * p;
A=p;//error, a is an array, not give him the assignment
P=a;//ok, make a pointer to a first address array p,
//you can understand it, the compiler to see such assignment, the array name when the right value (on the right hand side), the fixed action, put this into a address and then assigned to p

The void ysf (int [] b, int n1, int m1); Although grammar is so write,
Here you will certainly have a question, since the example above, the array name cannot as a value such as a=p, is wrong, here is b=a, don't also is no good?
This logic that's right, so, the compiler actually turned him into a void ysf (int * b, int n1, int m1) this form of
As for the language, can be written as void ysf (int [] b, int n1, int m1); So, just a better hint others, best is introduced into a continuous memory address here
That is to say, I might be in the function operation * (b + 1); , * (b + 2), is trying to suggest that the caller: "the caller, come in, so a separate your address may cross oh"

You want to have a feel the difference between array name and pointer variable
Int a, [10]. A just when compiling a label, you can be thought of as a virtual, only exist at compile time

The pointer variable
Int * p; He is a pointer variable, run time, a block of memory to hold the value of this variable
And any other variables, such as int I=1; You can imagine variables as memory corresponding to a grid,
Simply to imagine a box got;
CPU to memory address is nothing more than two kinds of operation, read and write, there are other? No
1. To write
int i=1;//I deposit is a special cosmetic box, you can give him 1, also can put 2
Int j=100;//~ · · · · · · · similar to the above
Int * p=& amp; i;//p is a special box, pointer (address) in here is to he put the I address, you can also put the address of the j in
P=& amp; j; This is just the box p, now he doesn't put the I address, become put the address of the j

2 for read
Printf (" % d \ n ", I);
I take out the contents of the box, to the terminal display
For machine, into the box (memory) to write, or read from, address is used to operate, the variable is programming language (for example, you now use of C language provides an abstract)
The compiler will put him into address, everything is address, on the whole,

-- -- -- -- -- --
However, int a, [10]. He represents the 10 continuous box, with int I; Compared to, he is just a bigger box
Maybe you want to use the above example to set, but once put a few good, if can of words, a=1, a=2, 3, 4,?????? Such operations where they put the good
Can't get, too much may be, there is ambiguity
So in this way a [0] represents the first grid, a [1] on behalf of the second so no ambiguity, according to the index to operate
People think you can imagine he is equivalent to define the int a0, a1, a2,,,, a9; 10 integer variables, and the 10 variable space is continuous
Leave out the trouble that you define 10 times, and the name of a role here is changed, only represent the first address, at the time of application, is actually a vanity,
You can think he disappeared, without his own box (memory), a [0] to [9] are inventories are via a data, can understand into the middle of the tool, he is just pure compile time

Probably more pull the more far, you see what insightful, take it

CodePudding user response:

refer to the second floor yukint response:
compiler is actually using Pointers instead of array name parameter
Array name a, itself represents an address first address (array), can be a pointer variable assignment to others, (this is the right value)
But you can't give his assignment
For example,
Int a, [10].
Int * p;
A=p;//error, a is an array, not give him the assignment
P=a;//ok, make a pointer to a first address array p,
//you can understand it, the compiler to see such assignment, the array name when the right value (on the right hand side), the fixed action, put this into a address and then assigned to p

The void ysf (int [] b, int n1, int m1); Although grammar is so write,
Here you will certainly have a question, since the example above, the array name cannot as a value such as a=p, is wrong, here is b=a, don't also is no good?
This logic that's right, so, the compiler actually turned him into a void ysf (int * b, int n1, int m1) this form of
As for the language, can be written as void ysf (int [] b, int n1, int m1); So, just a better hint others, best is introduced into a continuous memory address here
That is to say, I might be in the function operation * (b + 1); , * (b + 2), is trying to suggest that the caller: "the caller, come in, so a separate your address may cross oh"

You want to have a feel the difference between array name and pointer variable
Int a, [10]. A just when compiling a label, you can be thought of as a virtual, only exist at compile time

The pointer variable
Int * p; He is a pointer variable, run time, a block of memory to hold the value of this variable
And any other variables, such as int I=1; You can imagine variables as memory corresponding to a grid,
Simply to imagine a box got;
CPU to memory address is nothing more than two kinds of operation, read and write, there are other? No
1. To write
int i=1;//I deposit is a special cosmetic box, you can give him 1, also can put 2
Int j=100;//~ · · · · · · · similar to the above
Int * p=& amp; i;//p is a special box, pointer (address) in here is to he put the I address, you can also put the address of the j in
P=& amp; j; This is just the box p, now he doesn't put the I address, become put the address of the j

2 for read
Printf (" % d \ n ", I);
I take out the contents of the box, to the terminal display
For machine, into the box (memory) to write, or read from, address is used to operate, the variable is programming language (for example, you now use of C language provides an abstract)
The compiler will put him into address, everything is address, on the whole,

-- -- -- -- -- --
However, int a, [10]. He represents the 10 continuous box, with int I; Compared to, he is just a bigger box
Maybe you want to use the above example to set, but once put a few good, if can of words, a=1, a=2, 3, 4,?????? Such operations where they put the good
Can't get, too much may be, there is ambiguity
So in this way a [0] represents the first grid, a [1] on behalf of the second so no ambiguity, according to the index to operate
People think you can imagine he is equivalent to define the int a0, a1, a2,,,, a9; 10 integer variables, and the 10 variable space is continuous
Leave out the trouble that you define 10 times, and the name of a role here is changed, only represent the first address, at the time of application, is actually a vanity,
You can think he disappeared, without his own box (memory), a [0] to [9] are inventories are via a data, can understand into the middle of the tool, he is just pure compile time

May the more pull the more far, you see what insightful, take it

I feel benefit a lot. Thank you
  • Related