About the pointer to a function in C

Updated on technology 2024-04-10
6 answers
  1. Anonymous users2024-02-07

    c, the assignment operation requires that the lvalue is of the same type as the rvalue.

    The same type means that the variable itself is of the same type and the object it points to is of the same type, and both are indispensable.

    For functions, the return value is the same, and the parameter type and number must be the same.

    In addition to this, C also stipulates that variables must be defined before they can be used!

    In your example, 1) the return value is the same, the parameter type and number are the same, so the compilation passes!

    2) The return value is the same, the parameter type and number are the same, so the compilation can also pass!

    So I don't understand what kind of specific problem the landlord is compiling!

    Is the problem with the function being undefined?

    int cmp(const void*,const void*);This is just a statement, not a definition! You have to define it later.

    Also, the problem is related to the compiler's scanning process! This is a bit complicated, but you can get a general idea of how the compilation works!

    You can try adjusting p=cmp; The position of the statement, such as inside or outside of main, and see what the difference is.

  2. Anonymous users2024-02-06

    Here's what it looks like in general:

    typedef int (*p)(int ,int );

    p max;

    p is a pointer type, which is equivalent to the same effect as "int*" in "int* a".

    As for your situation, I can't explain it, you should post the error code.

  3. Anonymous users2024-02-05

    1. Suppose the function void f(int b).

    1) There is a definition int a[15], and f(a) is called, passing the first address.

    2) If you call f(&a[1]) is equivalent to call f(a+1), you will pass the address, not the entire array.

    Second, as mentioned in the first one, it is still the address.

    int arr[3][4];

    int (*a)[4] = arr;

    a points to arr[0], the address of the first line.

    A+1 points to arr[1], the second line address.

  4. Anonymous users2024-02-04

    1.Arrays are all passed pointers, so they are the first address.

    2.It's just the address of a[1].

    Address 4In relation to the number of dimensions, if it is one-dimensional, move to the second element.

  5. Anonymous users2024-02-03

    ...How do you look like you didn't write Jeon-eun.

    When used as arguments, int* a and int a are the same, both are the first address of the array.

    a[1] passes the address of the second element of the array. And just operate on the address of the second element, without opening up another space.

    2 If int* a is the address of A, 3 if a is int a[const]; a++ gets the address that is &a[1] plus an offset of the type size for each move.

    By the way, BS does not give points.

  6. Anonymous users2024-02-02

    The C++ pointer to a function is defined as:

    Return type. *pointer name) (list of function parameters), for example.

    void*p)(int) is to point to a return value of void

    A function whose argument is an int type.

    And how do you define a function pointer that points to a class member function? Use of member function pointers.

    1) Non-static member functions.

    Definition method: return type.

    Class name: *pointer name) (function parameter list) for example, void

    a::*p)(int) is a function pointer to a member function in class a.

    Assignment method: p=&a:: function name, while the general function pointer is assigned to p=function name, pay attention to the difference. (The member function must be of type public).

    Invocation method: The invocation of the member function pointer must be invoked through the class object, a*p(int) to call a member function (which is of type public).

    2) Static member functions.

    The definition and usage of static member functions are the same as those of ordinary function pointers, except that the method of assigning function pointers to non-static members is the same.

    This is because the pointer type of a static member function is the same as that of a normal function.

Related questions
6 answers2024-04-10

The p of both is a pointer.

p=&t, change the content that p points to to the address of t, and p=&t is the address that changes the pointer to t. >>>More

9 answers2024-04-10

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

11 answers2024-04-10

Valid variable names for the C language:

First, it can only contain numbers, letters, and underscores. >>>More

8 answers2024-04-10

You can use function pointers, such as:

voidfunction(int >>>More

7 answers2024-04-10

The pointer can be said to be a major feature of C, many people accidentally understand it, in fact, you can understand what the pointer is by comparing the visual method. When the data storage in the C language does not have a pointer to intervene, it is directly operated, just like if you go to a building to find someone, the security guards bring people directly to you, and C accesses the data as well, but sometimes you just don't know the name of this variable, that is, you don't know what the person's name is, can you still find the data or person, that is, you can use a pointer in C to achieve it, which is equivalent to you don't know what the person's name is, but you know that he lives in that room (let's assume that a room lives in a person), The security guard can still bring people to you, and the same goes for C, if you point out that the data is in that location, the CPU can also find the data for you, the same way. And what's the benefit of this.,It's going to talk about arrays.,You have a bunch of numbers stored in a continuous memory unit.,Similar to your group of friends living in that building.,And it's living next to each other.,You have to call them out to play.,Just like you want to take out the data and use it.,Then I would have shaken the registration word one by one to the security guard.,Say which person and that person and so on to help me find it.,C can also be achieved through this method.,But it's obviously too troublesome.,If you know the room number where the first person lives.,And then for example, there are 10 people, So you just need to say help me call the next number to the number of people (referring to the continuous room number they live in), oh, it's easier for the security guard to do, because the name of the security guard may not be known, but the room number must be known, the same is true in c, you don't need to call these data one by one, directly know the address of their first data, and then count down one by one to say that the data in that location can be called by me, in fact, you give a variable, the CPU still has to find that number through the address, It's like if you tell the security guard the name of that person, he still has to find out which room he lives in. >>>More