Thread: size of struct with pointers and function pointers

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    size of struct with pointers and function pointers

    Hi,

    I following structure

    Code:
    typedef struct
    {
    UINT32 abc;
    int (*f)(int a, intb);
    char *ptr;
    } my_struct;
    
    main()
    {
    my_struct myParams;
    sizeof(myParams);
    }
    Assume int is 4 bytes and char is 1 byte. I know the size of structure without pointers inside. But I am not sure with pointers and function pointers. Is it 4+4+4(12) or something else??

    Thanks in advance.
    Johnny

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    size of int + size of function pointer + size of char pointer (it's not a char you have, you have a char pointer, which is not 1) + any padding it feels like adding = size of your structure.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Thanks Quzah,

    I compiled and got these.

    sizeof(void*) is 4. Same way sizeof(char*) is also 4.
    The total size is 12 bytes = 4 for int + 4 for function ptrs +4 for char*.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that the size of any type is implementation defined. You can guess reasonably well on what size a structure and type may be, but you can never be sure. It changes from compiler to compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread