Home > Enterprise >  What is the difference between *(pointer) and (*pointer)?
What is the difference between *(pointer) and (*pointer)?

Time:03-19

Please see the following block of code. Can you tell me the difference between *(sample) and (*sample)?

for(i = 0; i < num_samples ; i   ) 
    {
        *(sample) &= 0xfff ;
        
        if( (*sample) & 0x800 ) 
            *(sample) |= 0xf000 ;
            *(sample 1) &= 0xfff ;
            
        if( *(sample 1) & 0x800 ) 
            *(sample 1) |= 0xf000 ;
            
        fprintf( my_data->fout, "%d, %d\n", *sample, *(sample 1) );
        
        sample  = 2 ;
    }

CodePudding user response:

This is purely a question about operator precedence

*sample , *(sample) and (*sample) all do this same thing in isolation. They deference the 'sample' pointer

Things get more interesting when combined with other operators. You have an example

 *(sample 1)

Lets take out the parens

  *sample 1  

This could mean two things

  • Give me the value pointed at by sample and add 1 to it
  • give me the value thats one after where sample points

Explictly bracketing that gives

  • (*sample) 1 => do the deref, then add 1
  • *(sample 1) => add 1 then deref

So what does

*sample 1

mean (ie with no brackets to dictate the order), not surprisingly it means

(*sample)   1

you can apply the same logic to all your other combinatons

  • Related