Thread: How to pass a structure member to a function?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29

    How to pass a structure member to a function?

    Code:
    struct msg_rsp
    {
      int count;
      struct info *info_entry;
    };
    
    void func()
    {
      struct msg_rsp *resp;
      
      for (int i = 0; i < resp->count;  i++)
        {
         // How to pass resp->info_entry here to the function func( ) & increment pointer?
          func ();  
        }
    
    }
    
    void func (struct info *entry)
    {
      ..
      ..
    }
    How to pass argument properly to func () and increment the pointer to next ?
    Last edited by krishnampkkm; 11-26-2020 at 10:01 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,166
    You practically answered your own question:
    Code:
    func(resp->info_entry);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29
    Ok. But how to increment pointer after calling function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,166
    Quote Originally Posted by krishnampkkm
    But how to increment pointer after calling function?
    A direct answer to your question is extremely simple: given a pointer p, you can increment the pointer in any one of at least four ways:
    Code:
    ++p;
    p++;
    p += 1;
    p = p + 1;
    But it doesn't sound like this is what you're looking for. Firstly, what pointer did you have in mind? You have the resp pointer and the resp->info_entry pointer, and it is not clear which you are referring to. Furthermore, it is not clear what "increment the pointer to next" means in this context. Is this supposed to be some kind of linked list or does the pointer in question point to an element within an array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,428
    Maybe
    Code:
      for (int i = 0; i < resp->count;  i++)
        {
         // How to pass resp->info_entry here to the function func( ) & increment pointer?
          func( &resp->info_entry[i] );  
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Replies: 3
    Last Post: 10-31-2013, 09:44 AM
  3. Replies: 1
    Last Post: 11-30-2012, 07:18 AM
  4. How to pass member functions into a function object...
    By TeenWolf in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 01:01 PM

Tags for this Thread