The difference and usage of ARM function pointer and pointer function

Publisher:一直333Latest update time:2020-07-23 Source: elecfansKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When I was learning ARM, I found that it was easy to confuse "pointer function" with "function pointer", so today, I want to clarify it once and for all, and I found some information and some summary from everyone, and I organized it here to share with you.


First, their definitions:


1. A pointer function is a function with a pointer, which is essentially a function. The function return type is a pointer of a certain type.

Type identifier*function name (parameter list)

int *f(x,y);

First of all, it is a function, but the return value of this function is an address value. The function return value must be accepted by a pointer variable of the same type, that is, a pointer function must have a function return value, and, in the calling function, the function return value must be assigned to a pointer variable of the same type.


express:

float *fun();

float *p;

p = fun(a);

Note the difference between pointer function and function pointer, and do not confuse them. The easiest way to distinguish is to see if the pointer * in front of the function name is contained in brackets (). If it is contained, it is a function pointer, otherwise it is a pointer function.


Let's talk about it in more detail! Please see below

Pointer function:

When a function declares that its return value is a pointer, it actually returns an address to the calling function for use in expressions that require a pointer or address.


Format:

Type specifier * Function name (parameters)

Of course, since what is returned is an address, the type specifier is usually int.


For example: int *GetDate();

int * aaa(int,int);

The function returns an address value, which is often used to return the address of a certain element in the array.


int * GetDate(int wk,int dy);

main()

{

int wk,dy;

do

{

printf(Enter week(1-3)day(1-4)n);

scanf(%d%d,&wk,&dy);

}

while(wk《1||wk》3||dy《1||dy》4);

printf(%dn,*GetDate(wk,dy));

}

int * GetDate(int wk,int dy)

{

static int calendar[3][4]=

{

{1,2,3,4},

{5,6,7,8},

{9,10,11,-1}

};

return &calendar[wk-1][dy-1];

}


The program should be easy to understand. The subroutine returns the address of an element in the array, and the output is the value at this address.


2. A function pointer is a pointer variable pointing to a function, that is, it is essentially a pointer variable.

int (*f) (int x); /* declare a function pointer */

f=func; /* Assign the first address of the func function to the pointer f */

A pointer to a function contains the address of the function, through which the function can be called. The declaration format is as follows:

Type specifier (*function name)(parameters)

Actually, this is not a function name, but a pointer variable name. This special pointer points to a function that returns an integer value. The pointer declaration should be consistent with the declaration of the function it points to.


The parentheses around the pointer name and pointer operator change the default operator precedence. Without the parentheses, it becomes a prototype declaration of a function that returns an integer pointer.


For example:

void (*fptr)();

Assigning the address of a function to a function pointer can be done in the following two ways:

fptr=&Function;

fptr=Function;

The address operator & is not required because a function identifier alone indicates its address. If it is a function call, it must also contain a parameter list enclosed in parentheses.


There are two ways to call a function through a pointer:

x=(*fptr)();

x=fptr();


The second format looks the same as a function call. However, some programmers prefer the first format because it explicitly indicates that the function is called through a pointer rather than a function name. Here is an example:

void (*funcp)();

void FileFunc(),EditFunc();

main()

{

funcp=FileFunc;

(*funcp)();

funcp=EditFunc;

(*funcp)();

}

void FileFunc()

{

printf(FileFuncn);

}

void EditFunc()

{

printf(EditFuncn);

}


The program output is:

FileFunc

EditFunc


The main difference is that one is a pointer variable and the other is a function. It is necessary to understand it clearly before using it correctly.

Keywords:ARM Reference address:The difference and usage of ARM function pointer and pointer function

Previous article:ARM Linux: Implementation principle of switching from usr mode to svc mode
Next article:Causes and treatment measures of ARM abnormal interruption

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号