-2

I've only been doing C for a few weeks so I am quite new to it.

I've seen things like

* (variable-name) = -* (variable-name) 

in lecture notes but what exactly would it do? Would it just negate the value being pointed to?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    Yes, it would. (So, that's not quite `pointer = -pointer`, but rather `pointed_object = -pointed_object`.) – The Paramagnetic Croissant Sep 06 '14 at 15:29
  • Thought it would have been something simple, and noted. Thank you for the quick response! – Conor Hillman Sep 06 '14 at 15:31
  • Note `*ptr *= -1;` is shorter. – wildplasser Sep 06 '14 at 15:37
  • @wildplasser Usually but not if the name of the variable is single letter: `*i = -*i;` (7 signs).... `*i *= -1;` (7 signs). I think you should adjust your comment. – Igor Pejic Sep 06 '14 at 15:47
  • 1
    Duh! I meant: easier to read. The human brain needs to lookup only one identifier, and does not need to check if LHS and RHS are equal. IMHO the -1 constant is so common that it will be recognised without lookup ... – wildplasser Sep 06 '14 at 15:56
  • Note that for simple pointer variables (e.g. `int *ptr`), the parentheses are unnecessary (`*ptr = -*ptr` is unambiguous). For more complex pointer expressions, the parentheses could be necessary: `*(ptr+3) = -*(ptr+3);`, for example, but then why not write `ptr[3] = -ptr[3];` anyway? However, the parentheses are not 'wrong'; they're merely unnecessary in the majority of cases. – Jonathan Leffler Sep 06 '14 at 17:28

2 Answers2

2

Yes. When you add the star it means it is pointing to the value. It is essentially saying: to the value at address (variable-name), become -1* the value of (variable-name).

If you are new to C, you may find it easier to use & instead of pointers. &'s are essentially the opposite of *. & doesn't point, it gives the address of a variable (which I find to be a simpler concept).

The following is an example which should demonstrate the use of * and &.

#include <stdio.h>

int main(void)
{
    int blah = 6;
    int *num = &blah;
    (*num) = -(*num);
    printf("%d\n", num); //Displays num
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zac Wimer
  • 89
  • 5
0

Yes it negates the value being pointed to. We cannot write operations on addresses so operations are done on deferenced pointers. Here, *d = -*c means:

*d = (-1) * (*c)

Sample code:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    int *c;
    int *d;
    d = (int *)malloc(sizeof(int));
    c = (int *)malloc(sizeof(int));
    *c = 9;
    *d = -*c;
    printf("%d", *d);
    free(c);
    free(d);
    return 0;
}
honk
  • 9,137
  • 11
  • 75
  • 83
MG K
  • 23
  • 10
  • Please add some whitespace. (Note: before K&R2 `*d=-*c;` had a different meaning) – wildplasser Sep 06 '14 at 16:22
  • @wildplasser: if you're thinking of the old-style assignment operators `=-` instead of `-=`, then even K&R 1 used the new versions. See [What does `=+` mean in C?](http://stackoverflow.com/questions/7573978/what-does-mean-in-c/7578156#7578156) – Jonathan Leffler Sep 06 '14 at 17:10
  • You say _We cannot write operations on addresses_, but that's not entirely accurate. You can add or subtract an integer from an address, or take the difference of two addresses (to two objects in the same array), but you can't multiply or divide addresses, nor can you negate them. – Jonathan Leffler Sep 06 '14 at 17:13
  • @JonathanLeffler [yes] IIRC it was (at least) still accepted by compilers, often with a warning. Not too sure about K&R1, though. – wildplasser Sep 06 '14 at 17:19
  • @wildplasser: I believe your compiler would have had to be older than 1980 for it to accept `=-` as the modern `-=`. The old notation was phased out before AT&T 7th Edition UNIX was released in 1979, and before K&R 1st Edition was published. I've not double-checked Lyons analysis of 6th Edition UNIX; I'd not be very surprised to find the old notation was used in that. – Jonathan Leffler Sep 06 '14 at 17:31
  • My memory may be fogged. Could even be that the acceptance of `=-` needed an explicit flag. Trying to dig up K&R1 from the web. – wildplasser Sep 06 '14 at 17:52