Search code examples
cansi-c

Whats the difference between *(a+b) and (*a+b)


A friend helped me with this code and I don't understand how it works. in the 5th line is (*pp-97)*4 basically the size of the char 110 so (110-97)*4 or the scanned value of pp? Thanks

char *pp =(char*)malloc(110);
printf("Enter text: ");
scanf("%s", pp); 
*pp = *(pp + n); 
int f = (*pp - 97)*4;

Solution

  • Note that *pp is equivalent to pp[0], and generally *(pp + n) is equivalent to pp[n], so

    *pp = *(pp + n);
    

    could also be written pp[0] = pp[n];, that copies the char at offset n to the first char at offset 0.

    int f = (*pp - 97)*4;
    

    and this could be written

    int f = (pp[0] - 97)*4;
    

    so 97 (the ASCII value of 'a') is subtracted from the first char in the block pp points to, and that difference is multiplied with 4.