Why following code not work

Discussion in 'General Electronics Chat' started by aamirali, Dec 24, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Code ( (Unknown Language)):
    1.  uint8_t y[4];
    2.  
    3. void call(uint8_t *z)
    4. {
    5.     *((uint8_t *)(&z)) = 10;           //don't change value here
    6.                                     // & on compiling gives warning
    7.                         //warning:  #550-D: parameter "z" was set but never used
    8.    
    9.      *z =10;                  //this works fine
    10.  
    11.  
    12.  
    13. }
    14.  
    15.  
    16. int main (void)
    17. {
    18.     call(y);
    19.     while(1);
    20. }
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    It's because you are dereferencing the pointer, z.

    You are already passing in the pointer as a uint8_t *, so writing (&z) dereferences the pointer to a pointer to the location of a pointer. leave out the & and you'll be fine...

    ...well, I assume you are trying to initialize this array, if so, you'll need to iterate over the elements.
    if so, try this:
    Code ( (Unknown Language)):
    1. uint8_t y[4];
    2.  
    3. void call(uint8_t *z)
    4. {
    5. for(int i = 0;i<4;i++)
    6. {
    7.     *((uint8_t *)(z+i)) = 10;
    8. }
    9.  
    10. }
    11.  
    12. int main (void)
    13. {
    14.     call(y);
    15.     while(1);
    16. }
    Tidbit: since you aren't casting the pointer to another type, why are you casting it at all?

    Also, this should be located in the Programmer's corner section of the forum, not here....
     
    Last edited: Dec 24, 2012
  3. WBahn

    Moderator

    Mar 31, 2012
    17,726
    4,788
    If you define a function

    Code ( (Unknown Language)):
    1.  
    2. int fred(double x)
    3. {
    4.     x = 10;
    5. }
    6.  
    7. You will probably get a similar warning.
    8.  
    9. 'x' is initialized with a value that is passed into the function. You then overwrite it without using the original value -- that is 'probably' a logic error, so many compilers will throw a warning. But it's also possible that the warning is for the second offense, namely you set a local variable to a value and then don't use that value before it goes out of scope. Again, probably a logic error that the compiler is trying to give you a heads up about.
    10.  
    11. I'm assuming, when you say that the second line worked, that you didn't actually run the function with both lines in it and saw it work.
    12.  
    13. The first line is identical to writing
    14.  
    15. z = 10;
    16.  
    17. Which throws away the value that is passed into the function (namely the base address of the global y array) and says that there is now a variable (or array) of type uint8_t stored at memory address 10. A subsequent attempt to use this value as such should have caused all kinds of problems.
    18.  
    19. Assuming the first line was commented out when the second one appeared to work, the reason is that the second one is the same as if you had written
    20.  
    21. z[0] = 10;
    22.  
    23. The notation
    24.  
    25. a[b] = c;
    26.  
    27. is the same as
    28.  
    29. *(a+b) = c;
    30.  
    31. Where either 'a' or 'b' has to be a pointer (other than a void pointer) and the other has to be an integer (of some flavor). It says to calculate the address by taking the integer, multiplying it by the size of whatever type of object is pointed to by the pointer (which is why it can't be a void pointer) and adding that to the value of the pointer. The store the value of c, using the representation of the type of object pointed to by the pointer, at that address.[/b]
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    What in the world is this supposed to do:
    Code ( (Unknown Language)):
    1. *((uint8_t *)(&z)) = 10;
    I'm confused too. What was your intent?
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,870
    996
    And I thought it was just me not understanding some kind of super sophisticated C coding. :)
     
Loading...