Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5839881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:39:18+00:00 2026-05-22T11:39:18+00:00

I came across the following code while debugging. I was able to get the

  • 0

I came across the following code while debugging. I was able to get the result correctly but I didn’t not understand the function pointers defined in here.

Here pNewMsgeFunc is the alias name of the function pointer tNewMsg which is created inside the structure stRsStruct.

RsMsg.h

typedef RsMsg* (*tNewMsg)(void);
tNewMsg    pNewMsgFunc;
typedef struct
{
    int         nMsgId;       
    NSString*     sAsciiName;       
    tNewMsg    pNewMsgFunc; // calls new for the particular message
} stRsStruct;

RsMsg.cpp

RsMsg(int uMessageId,tNewMsg pNewMsg,const char* szAsciiName,void* pData,
      size_t uDataSize)
{
 //intialisations
}

RsMsgDerived.h

#define DECLARE_NEWMSG(CssName,CssID)                   \
    static CssName* FromMsg(RsMsg* pMsg)                \
    {                                                   \
        return dynamic_cast<CssName*>(pMsg);            \
    }                                                   \
    static RsMsg* NewMsg()                              \
    {                                                   \
        return new CssName;                             \
    }                                                   \
    enum {ID = CssID};                                  \

Its said that in the structure pNewMsgFunc will point to the NewMsg() function.

But I could not get it how its possible without intialising tNewMsg with the address of

the NewMsg() function.But this code is running fine.There is no other constructors used to

intialise the function pointer with the address of the function NewMsg().

Event.cpp

#import "RsMsg.h"
#import "RsMsgDerived.h"

RsMsg* RsMsg::CreateMessage(REMOTE_MESSAGE_ID nMessageNumber)
{
    RsMsg*       pMsg = NULL;
    stRsStruct*  pMsgStruct;

    pMsg = pMsgStruct->pNewMsgFunc();    //Invoking the function pointer
}

Here by invoking the function pointer I’m calling the static NewMsg() function.

But how this function gets called as pNewMsgFunc() is not assigned the address of NewMsg.

I need to call the NewMsg() function through pNewMsgFunc(). Is there any change to be made in the above code?

EDITED:

How to implement the same code in Objective C. This function pointer calls a function
whose return type is a class.So though the function pointers can be implemented in C as
its calling a function whose return type is a class cannot be implemented in Objective C
as c function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:39:19+00:00Added an answer on May 22, 2026 at 11:39 am

    Here pNewMsgeFunc is the alias name of the function pointer tNewMsg which is created inside the structure stRsStruct

    No, it’s not. tNewMsg is a name that identifies a type.

    typedef RsMsg* (*tNewMsg)(void);
    

    …but pNewMessageFunc is an object of that type.

    Look at it this way. In this code:

    typedef unsigned int uint;
    uint n = 42;
    

    n is not an alias of uint. Rather, n is a variable of type uint.

    So, pNewMessageFunc is a global variable (of type pointer-to-function-which-takes-no-parameters-and-returns-pointer-to-RsMsg) which you never initialize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While reading a code I came across, the following definition and initialization of a
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Today at work we came across the following code (some of you might recognize
Whilst trawling through some old code I came across something similar to the following:
I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...
While developing part of a simulator I came across the following problem. Consider a
So while rewriting some code, I came across something along the lines of: Method
While moving some code around for investigation purposes, I came across a little feature
I came across the following markup in a JSP file in a legacy app
I'm trying to debug an application (under PostgreSQL) and came across the following error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.