-1

[image of an executed code in keil]

I've written above code to move an array( msg[ ] ) to Port 0(I'm using Keil µvision), in which 'unsigned int y' gets the size of the array and displays it to Port 1(Port-1 output is shown in the image, whereas Port-0 output is not)

Question:

  1. Why am I getting double the value of the specified array?
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Arshdeep Singh
  • 316
  • 4
  • 15
  • 2
    Post code as text to improve clarity. – chux - Reinstate Monica Apr 25 '18 at 03:40
  • I've wanted to show output of port with the code – Arshdeep Singh Apr 25 '18 at 03:43
  • You may need `sizeof(msg)/sizeof(msg[0])`. – Stan Apr 25 '18 at 03:50
  • @ArshdeepSingh : But how is the output port relevant to teh size of the array. You have not even specified the size you are getting - I would _expect_ on an 8051 platform - you should also specify your platform - I have inferred 8051 from the header file included, if it were not for that I'd have assumed ARM since that is Keil's primary product since there were acquired by ARM. `sizeof` an array is not the same as the number of elements. Your expression `sizeof(msg)/2` is ill advised - better `sizeof(msg)/sizeof(*msg)` which will be portable regardless of the platform. – Clifford Apr 25 '18 at 09:07
  • @ArshdeepSingh ; Ah - I see that you are using the port to "display" the size - You could have used a debugger watchpoint or just said that `y == 8` - we'd have believed you. – Clifford Apr 25 '18 at 10:22

1 Answers1

2

Your message is not at all clear, but I believe you are getting 8, when you expect 4.

sizeof() returns the number of BYTES in a variable.
If unsigned int is 2-bytes on this platform, then an array of 4 ints has a size of 8 bytes.

abelenky
  • 63,815
  • 23
  • 109
  • 159