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

  • SEARCH
  • Home
  • 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 9158997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:23:09+00:00 2026-06-17T13:23:09+00:00

I would like to simulate the object oriented programming, so in C++, let’s consider

  • 0

I would like to simulate the object oriented programming, so in C++, let’s consider the following C code:

typedef struct tAnimal{
   char * name;
   int age;
}tAnimal;

typedef struct tAnimal2{
   char * name;
   int age;
   float size;
}tAnimal2;   

In C++ you can create a table of different objects which are inherited from the same class.
I would like to do the same in C, let’s consider the following code:

tAnimal ** tab;
tab = malloc(sizeof(tAnimal*)*2);
tab[0] = malloc(sizeof(tAnimal));
tab[1] = malloc(sizeof(tAnimal2));

Notice that the allocation works because malloc returns a void pointer, and C does not require casting. But I still have no access to the size field, because the type of tab elements is tAnimal after all.

Is there anyway to fix this?, I would like to stay away from void ** pointers.

  • 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-06-17T13:23:10+00:00Added an answer on June 17, 2026 at 1:23 pm

    If you want to access the size field you have to cast your pointer to tAnimal2. Note, the same would be true for C++.

    You can kind of simulate inheritance by embedding the first struct at the beginning of the second:

    struct tAnimal{
       char * name;
       int age;
    };
    
    struct tAnimal2{
      struct tAnimal parent;
    
      float size;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to simulate the user pressing tab then enter when they press
I am rendering textured quads from an orthographic perspective and would like to simulate
Would like to know the c# code to actually retrieve the IP type: Static
I would like to simulate dropping stones into a bucket on the web. Drag
I am developing a Flex application in which I would like to simulate roughly
I would like to simulate keystrokes within an embedded System.Windows.Controls.WebBrowser . Various techniques for
I would like to simulate shortcuts for a remote control feature of our Windows
I wrote my own object Tag and I would like to to contains if
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden

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.