Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 25-06-2008 10:32PM
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I was wondering if any of you have an explanation for this.
    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:
    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.


Comments

  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Thinking about this some more, I can see it's the size of the pointer to the array I'm seeing in the multiply function.

    Does anyone know how I can get it to output the size of the array?


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    The first sizeof is getting the size of an array, which is 3, the sizeof in multiply is getting the sizeof a pointer to an unsigned char.

    EDIT, Oh you realised... There isn't really a way to tell the length in C or C++, You'd have to pass size values to the multiply function.


Advertisement