-2

I have been doing a lot of research and also playing around with code, but I'm still confused with this: Why is case 1 erroring?

Case 1:

int arr[] = {0,4,7};
int *p = arr[0]; // pointer points to the first element in the array

printf("%d", ++*p);
// This results in Segmentation fault

Case 2:

int arr[] = {0,4,7};
int *p = arr; // pointer points to the first element in the array

printf("%d", ++*p);
// This prints 1
Passer By
  • 19,325
  • 6
  • 49
  • 96
Fred Vann
  • 19
  • 6
  • 5
    `int *p = arr[0];` should be `int *p = &arr[0];` `arr[0]` isn't the address of the first element. It's the first element. If you want the address of the first element you need the `&` operator. `arr[i]` is synonymous with `*(arr + i)`. There was a recent CPP con video on pointers (like... two days ago) by Ben Saks. If you're on this subject, [take a look](https://youtu.be/rqVWj0aVSxg). – JohnFilleau Sep 29 '20 at 02:34
  • @JohnFilleau OH. I see. Thank you! – Fred Vann Sep 29 '20 at 02:36
  • The [code](https://godbolt.org/z/znqcr9) doesn't even compile, so there's no question of segfaults. Please paste the exact code that gives the error. – cigien Sep 29 '20 at 02:36
  • arr[0] is the first element in the array, also it is a pointer, points the first. But arr is not just a pointer. – AleXelton Sep 29 '20 at 02:37
  • 1
    Also this is duplicated question. Please reference on here. https://stackoverflow.com/questions/33035068/difference-between-array-and-array0 – AleXelton Sep 29 '20 at 02:38
  • 2
    @G.Alexander — `arr[0]` is **not** a pointer. – Pete Becker Sep 29 '20 at 02:41
  • 1
    `int *p = arr[0];` is incorrect code, if you do not see an error message then adjust your compiler settings as it causes you to waste time by not stopping at this error . – M.M Sep 29 '20 at 03:04

3 Answers3

2

In case 1: arr[0] is an int value. int *p = arr[0]; is an error.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Allen ZHU
  • 690
  • 5
  • 14
1

What is the difference between these two pointer assignments in C?

Firstly, one thing common with both is that neither is an assignment.

int *p = arr[0]; // pointer points to the first element in the array

The comment here is wrong. arr is an array of int. arr[0] is the first element of that array. It is an int. int *p = arr[0] is an attempt at initialising a pointer with the value that is stored in the array. However, the type doesn't match since the value is not a pointer. int is not implicitly convertible to int* and therefore the program is ill-formed.

A standard conforming language implementation is required to diagnose this issue for you. This is what my compiler says:

error: invalid conversion from 'int' to 'int*'

Why is case 1 erroring?

If the program even compiles, you are initialising a pointer with the value 0. This likely means that it is a null pointer and that it doesn't point to any object. ++*p attempts to indirect through this null pointer and access the non-existing integer.

int *p = arr; // pointer points to the first element in the array

Comment here is correct.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

I guess it is throwing an error because you did not use '&' before assigning the value to the pointer. As it is a pointer so you can not assign a value to it. You can only use it to store addresses. I coded it in C++ and here is the correct form.I guess it helps.

#include <iostream>

using namespace std;

int main(){
  int arr[] = {0,4,7};
  int *p = &arr[0]; //you are missing '&' before array[0]

  printf("%d", ++*p);
  system("pause");
  // This prints 1
  }
Dead Guy
  • 57
  • 1
  • 9