How do I use a pointer as a parameter?

Updated on technology 2024-04-01
9 answers
  1. Anonymous users2024-02-07

    b is the pointer, *b is the value in memory, temp=*b, the variable value is given to the variable, correct.

    If you define temp as a pointer, int *temp, where temp is the pointer, temp represents the address, *b is the variable value, and the variable value instead of the address is of course incorrect, and the same is true for others.

  2. Anonymous users2024-02-06

    int * temp;

    This is to define a pointer variable, the variable name is temp, and it can store an address;

    int temp;

    This is to define an integer variable, the variable name is temp, and it can store an integer;

    P1 is not an address, it is the value of an integer.

    p1 should be a pointer variable, and * is an indirect access operator, which is used to access the storage cell according to the address stored in p1, and the storage unit represents the value stored in it when it is used.

    So *p1 is an integer value.

  3. Anonymous users2024-02-05

    You don't know the pointer, and you don't have an address for the *temp you declare, do you know where he's pointing? The pointer without the assignment points to the place is immediate, in case your pointer is pointing to the operating system of your machine, you are changing the parameters in the operating system, you are ready to reinstall the operating system, people declare that temp is only used as a transfer station to exchange the value of 2 pointers, so that the value of their 2 will not be lost because of one assignment to the other.

  4. Anonymous users2024-02-04

    You misunderstood. A pointer is the address (the variable it points to). b is the address of a variable, and *b is a dereference to the pointer and takes out the value of the address it represents, which is equivalent to the value in the room where the key is opened.

    temp=*b ;= Both sides are int type.

    temp=b ;The left is an int and the right is an address instead of an int value.

  5. Anonymous users2024-02-03

    1. The length of the array is generally defined by Hongpi Zheng, which is convenient for modification when the subsequent project is large.

    2. Try to have some error control in the program, that is, consider improving it, such as the judgment of whether the above memory allocation is successful and the judgment of whether the expected purpose is achieved after the function is called.

    3. Use typedef to simplify the definition.

    4. Using null ==pps, instead of pps ==null is a good programming habit, which will also be asked in some interviews, the reason for this is because when "== is mistakenly written as "=", the former cavity spring directly compiled the error, the latter can be run, this kind of hidden error can lead to chaos in the whole project, this kind of bug is sometimes difficult to find, so this habit is very important, a sentence is "when judging, write constants on the left side of the equal sign, Write variables to the right of the equal sign to prevent serious bugs caused by writing one less equal sign";

    5. Finally, you must remember to release the memory after using the pointer, otherwise there will be problems such as memory leakage.

  6. Anonymous users2024-02-02

    In C, the parameters of a function can not only be specific data such as integers, decimals, characters, etc., but also pointers to them. Using a pointer variable as a function parameter can pass an address outside the function to the inside of the function, so that data outside the function can be manipulated inside the function, and the data will not be destroyed with the end of the function.

    Arrays, strings, dynamically allocated memory, etc., are all sets of data that cannot be passed into the function through a single parameter, but can only pass their pointers, and influence these data sets through pointers inside the function.

    Sometimes, pointers are also necessary for operations on basic types of data such as integers, decimals, characters, etc., a typical example is to exchange the value of two variables.

    When the swap() function is called, the addresses of the pickers of variables A and B are assigned to P1 and P2 respectively, so that *p1 and *p2 represent the variables A and B themselves, and the values of *p1 and *p2 are exchanged for the values of A and B.

    Although p1 and p2 will be destroyed after the function runs, its impact on external A and B is "persistent", and it is not imperative that it will "return to the original state" with the end of the function.

    Note that the temporary variable temp is particularly important because executing *p1 = p2; The value of a will be overwritten by the value of b, and you won't be able to find it if you don't save the value of a first.

  7. Anonymous users2024-02-01

    A pointer is actually an address, for example.

    inta=3;

    intp=&a;

    At this time, p is a pointer, and its value is the address of a in memory, & is the meaning of taking the address, and the pointer is used as a form parameter in the function call, so when the function is called, the real argument is passed to the function is the address of the parameter, and the function will be sensitive to the variable according to the address passed over in the process of execution. Finally, the value of this variable will be changed.

    This transitive method is also implemented with an address by reference.

    Without a pointer variable, the function will open the memory in another part of the memory when it is called, and the passed value will be handed over to the newly opened memory. During the execution of the function, it only operates on this part of the newly opened memory variable, and will not return the value after the end, so the pointer variable is actually changed by the address to change the value of the variable in the main function.

  8. Anonymous users2024-01-31

    It's just that in your question, the pointer is pointing to and referencing another pointer variable. Suppose the pointer of the pointer is pp, the reference of the pointer is r, and the pointer being pointed to is p, then:

    int a;Target integer variables.

    int *p=&a;First-level pointer.

    int **pp=&p;Secondary pointer.

    int &r=&p;Pointer references.

    Their relationship is represented by logical notation as:

    pp->p->a (the value of pp is the address of p, and the value of p is the address of a, that is, pp points to p, p points to a, and pp itself also needs to occupy memory space, so pp also has its own address).

    r=p->a (r refers to the address of p, the value of p is the address of a, that is, r is an alias of p, p points to a, where the value of r is the address of p, and r itself does not need to occupy memory space, so it does not have its own address).

    Therefore, the difference between a reference and a pointer variable is that the reference itself does not occupy memory space, and the pointer variable needs to occupy memory space (all variables need to occupy memory space, and everything that occupies memory space must have an address).

    If you still don't understand, you can ask.

  9. Anonymous users2024-01-30

    c ?The essence of the C language is the pointer, which is also the most difficult to understand, and the system can easily collapse if it is not used well.

Related questions
12 answers2024-04-01

For each router, there is a console interface through which the administrator can connect the control port to the main port of a comma terminal through the RS-232 cable, and configure the router through this terminal. >>>More

11 answers2024-04-01

The Honor 50 Pro phone is very good, and here are some of the parameters of this phone: >>>More

3 answers2024-04-01

** amount of control, shooting fog scenes usually adds 1 2 stops ** to the metering data in the camera. Depending on the intensity of the fog and the amount of white on the screen, it can be adjusted even more. In this way, the fog can be whitened and the layers will not be lost. >>>More

4 answers2024-04-01

Graphics card selection reference scheme.

Contents: Graphics card main entry. >>>More

10 answers2024-04-01

Screen size ** branding.

=Personal Signature*****== >>>More