-1

I have read answers to questions like why a pointer can be assigned value? however I'm still a bit confused about changing the values of pointers. Some of the comments don't seem to be completely accurate, or maybe it's implementation specific. (example: a[1] is exactly the same as writing *(a + 1). Obviously you can write *a = 3 since a is int*, so you can also write *(a + 1) = 3, so you can also write a[1] = 3).

Writing *a = 3 produces a warning: initialization makes pointer from integer without a cast. As well as a segfault.

My question is as follows.

int main(void)
{
    int b = 5, c = 10;
    int *a = b;

    *a = c; /* Will not work. (Should technically change the value of b to 10, leaving the pointer still pointing to b.) */
    printf("%d\n", *a);
    return 0;
}

The above example will not work, and will produce a segfault, but the following works for some reason I am not aware of.

int main(void)
{
    int a[10], i;
    for (i = 0; i < 10; ++i) {
        *(a + i) = i; /* Supposedly the same logic as '*a = c;', but works*/
    }

    for (i = 0; i < 10; ++i) {
        printf("%d\n", *(a + i));
    }

    return 0;
}

Thanks for your time and efforts.

**EDIT: Thank you for the answers, since it is *a = &b (I knew this (typo), but now the second example with the loop is unclear), the array indices are treated as variables, not as addresses I presume?

Community
  • 1
  • 1
Byte
  • 509
  • 6
  • 15
  • `*` has three different meanings that are dependent on the context in which they are used. See http://stackoverflow.com/questions/36962658/what-exactly-is-the-purpose-of-the-asterisk-in-pointers/36962697#36962697. – R Sahu May 10 '16 at 04:38
  • Do not ignore compiler warnings. Warnings are errors unless you can *prove* otherwise. It makes no sense to discuss what a program with a compilation error does. – n. m. could be an AI May 10 '16 at 04:43
  • 1
    `*a = 3;` does not make a warning. Also, `*a = 3;` does not appear in your code. `*a = c;` is correct and does not give a warning. I guess you wrote `int *a = 3;` in your practice and didn't realize that this is different to `*a = 3;` . – M.M May 10 '16 at 04:58
  • What is unclear with the second example? – Rishikesh Raje May 10 '16 at 04:58
  • It is clear now.Thank you @M.M – Byte May 10 '16 at 05:00

1 Answers1

3

This:

int b = 5, c = 10;
int *a = b;

Doesn't work. You think the second line means this:

int *a;
*a = b;

But it doesn't. It means this:

int *a;
a = b;

The above error means that when you do this:

*a = c;

Your program crashes (or maybe not, undefined behavior!), because a is an invalid pointer (it points to address 5, which is wrong in many ways).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm aware that it means `a = b`. It's just shorthand declaration and assignment. The question was about mutating `a` by dereferencing it and assigning the value of c to its dereference (b). – Byte May 10 '16 at 04:36
  • @Byte: I've expanded my answer. Please see above. – John Zwinck May 10 '16 at 04:38
  • I've made a small error in my question, my apologies. But it invalidates my question. – Byte May 10 '16 at 04:42