Why following code not work

Thread Starter

aamirali

Joined Feb 2, 2012
412
Rich (BB code):
 uint8_t y[4];

void call(uint8_t *z)
{
    *((uint8_t *)(&z)) = 10;           //don't change value here
                                    // & on compiling gives warning
                        //warning:  #550-D: parameter "z" was set but never used
    
     *z =10;                  //this works fine
  


}


int main (void) 
{
    call(y);
    while(1);
}
 

tshuck

Joined Oct 18, 2012
3,534
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:
Rich (BB code):
uint8_t y[4];

void call(uint8_t *z)
{
for(int i = 0;i<4;i++)
{
    *((uint8_t *)(z+i)) = 10;
}
  
}

int main (void) 
{
    call(y);
    while(1);
}
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:

WBahn

Joined Mar 31, 2012
30,060
Rich (BB code):
uint8_t y[4];

void call(uint8_t *z)
{
    *((uint8_t *)(&z)) = 10;   //don't change value here
                               // & on compiling gives warning
                               //warning:  #550-D: parameter "z" was set but never used
     *z =10;                   //this works fine
}


int main (void) 
{
    call(y);
    while(1);
}
If you define a function

Rich (BB code):
int fred(double x)
{
    x = 10;
}

You will probably get a similar warning.

'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.

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.

The first line is identical to writing

z = 10;

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.

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

z[0] = 10;

The notation

a = c;

is the same as 

*(a+b) = c;

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.
 

ErnieM

Joined Apr 24, 2011
8,377
What in the world is this supposed to do:
Rich (BB code):
*((uint8_t *)(&z)) = 10;
I'm confused too. What was your intent?
 
Top