Sizeof array increases by one byte after passing it to a function

This is a discussion on Sizeof array increases by one byte after passing it to a function within the C Programming forums, part of the General Programming Boards category; Hello I was wondering if any of you have an explanation for this. Code: int main() { unsigned char a[] ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Sizeof array increases by one byte after passing it to a function

    Hello

    I was wondering if any of you have an explanation for this.

    Code:
    int main()
    {
    	unsigned char a[] = {0x00, 0x80, 0x81};
    	unsigned char b[] = {0x00, 0x18, 0x00};
    
    	printf("sizeof a: %d\n", sizeof a);
    
    	multiply(a, b, f);
    }
    This outputs the text "sizeof a: 3".

    The multiply function is then called:

    Code:
    void multiply(unsigned char *a, unsigned char *b) {
    
    	printf("sizeof a: %d\n", sizeof a);
    }
    This outputs the text "sizeof a: 4".

    ???

    Can any of you think of an explanation for this?

    Thanks in advance.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    a is a pointer inside of multiply, so it's giving you the size of the pointer (4 bytes).

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Ah, thank you, that makes sense.

    Do you know how I can get sizeof to return the size of the array instead of the pointer?

    Thanks.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you can't. you will have to keep track of it manually.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    OK, thanks, at least I now know what to do.

    Cheers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    Another note, though:
    Since a and b are pointers, you will need to dereference them to get the real type.
    So that would be sizeof(*a). Unfortunately, it will return 1, since C does not keep track of an array once you pass it as a pointer.
    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. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 10:57 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21