0

I'm learning C through learn-c.org and sometimes they use the * operator before writing something (like specifying a data type) and other times they put the asterisk at the end. I read that * was the operator that de-references a variable. What is the difference in the placement of *?

some examples from the website:

void addone(int * n) {
    (*n)++;
}

and

char vowels[] = {'A', 'E', 'I', 'O', 'U'};
char *pvowels = &vowels;
int i;

or

int compare(const void* left, const void* right) {
    return (*(int*)right - *(int*)left);
}
mlwn
  • 1,156
  • 1
  • 10
  • 25
VeryNiceCoat
  • 69
  • 1
  • 1
  • 6
  • You might find your answer at this previous SO answer https://stackoverflow.com/questions/180401/placement-of-the-asterisk-in-pointer-declarations – pastaleg Mar 19 '20 at 16:57
  • 1
    @pastaleg: I don't think that's right, the question is about the difference between `*` as a type specifier and an operator. – Mat Mar 19 '20 at 16:58
  • @Mat I think you should double check. Its clearly about pointers – pastaleg Mar 19 '20 at 17:00
  • https://stackoverflow.com/a/5487780/635608 is probably more what OP is asking about – Mat Mar 19 '20 at 17:03
  • 1
    As a unary operator, `*` always comes before. In the case of an abstract declarator it appears to come after because the item it acts on is implicit. For instance, `(int *)` is an abstract declarator, but it's the same as `int *x` with the variable name `x` removed. – Tom Karzes Mar 19 '20 at 17:03
  • 1
    @pastaleg: there is a near infinite amount of C questions about pointers. Not all are identical. – Mat Mar 19 '20 at 17:04
  • @VeryNiceCoat: you should also look at the "right-left" rule: http://cseweb.ucsd.edu/~ricko/rt_lt.rule.html – FoggyDay Mar 22 '20 at 17:25

0 Answers0