0

I need help in resolving an issue which i am facing .

I would want to have a function prototype which accepts two different types of argument at two instances.

For example shown below: I have a structure as shown below:

         typedef struct BufferStruct
         {
            unsigned long    BufferType;
            unsigned long    TheAddress;
          } Buffer;

I have a function named Test and this function should accept two different types of argument

  1) std::array<Buffer, 2> axBuffer; 
  2) Buffer axBuffer;

   void Test(??)/* function argument ?*/

 These are C functions and not C++.

 Can someone please help me in getting the appropriate function prototype for the function named "Test"?

 Advanced thanks.
1
  • 3
    There is no function overloading in C. It's not possible. Commented Jun 6, 2020 at 14:18

1 Answer 1

6

C doesn't have function overloading. The simplest way to approach this with a single function is to take a pointer-to-Buffer and a length:

void Test(Buffer *buffer, size_t length);

When invoking this function with a single Buffer:

Buffer buf = ...;
Test(&buf, 1);

When invoking it with an std::array:

std::array<Buffer, 2> buffers = ...;
Test(buffers.data(), buffers.size());

If you want the length argument to be optional, you have two use two differently-named functions, unfortunately:

void TestOne(Buffer *);
void TestMany(Buffer *, size_t);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.