-2
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *p, n, i;
    printf("Enter the size of the array:");
    scanf("%d", &n);
    p = (int *)malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
    {
        printf("\n Enter element %d:", i + 1);
        scanf("%d", &p[i]);
    }
    for (i = 0; i < n; i++)
        printf("\n %d", p[i]);
    return 0;
}

Why do we need to write & in the scanf, if it's an array it's not necessary? p is a pointer to all the memory spaces so &p[i] should give the address of the pointer but not where we want to store the data right? Also if we write *p[i] in printf, it gives an error, p is a pointer so we should deference it and store the data in the reserved space in the memory but it's not working? Even if I compile the above program as it is it stops working after taking 3 values as input.

Rooney10
  • 31
  • 5
  • 1
    [Don't cast the result of malloc](http://stackoverflow.com/q/605845/296974). – glglgl Dec 12 '14 at 13:29
  • What is the exact input you are giving the program? What is the exact results you are getting? – David Schwartz Dec 12 '14 at 13:30
  • "`p` is a pointer so we should deference it": We do so, by using `p[i]` which is the same as `*(p + i)`. – glglgl Dec 12 '14 at 13:30
  • @DavidSchwartz no. of elements:5 I could enter ele 1,ele 2 and ele 3 but then it displayed program not working and it stopped. – Rooney10 Dec 12 '14 at 13:48
  • @glglgl if p is a pointer then &p[0] should give the address of the pointer which is pointing the element[0] and not the memory address of element[0]? – Rooney10 Dec 12 '14 at 13:48

4 Answers4

2

If the value is p[i]

p[i] = *(p+i)

Then the location should be

&p[i]

So you need to specify what is the location to which you need to store this value and it will be &p[i]

or (p+i)

&p[i] = (p+i)
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    don't overcomplicate things: `&p[i] == &*(p+i) == (p+i)` – BeyelerStudios Dec 12 '14 at 13:34
  • @BeyelerStudios I have put it in another way !! I think nothing is complicated here – Gopi Dec 12 '14 at 13:36
  • you just added a mistake: `&(p+i)` is not the same as `(p+i)` – BeyelerStudios Dec 12 '14 at 13:39
  • @BeyelerStudios Oops yes fixed it – Gopi Dec 12 '14 at 13:44
  • @Gopi When we dynamically allocate memory using malloc do we create an array with a certain no. of reserved space or a pointer which points to a reserved space? If it's a pointer, we find the address of the pointer using &p? – Rooney10 Dec 12 '14 at 14:10
  • @Rooney10 You will be having pointer to the first location of the memory which was allocated by `malloc()` in this case let's say that is `p` then `p+` will be the next block in that allocated memory chunk.You need a double pointer to store the address of the pointer – Gopi Dec 12 '14 at 14:18
2

The subscript operator [] also performs dereferencing. It performs indexing and dereferencing at once. p[n] is equivalent to *(p+n), i.e. it goes to an offset and dereferences it. Consequently, &p[n] is equivalent to p+n.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • It's the subscript operator, not the dereferencing operator. (There isn't anything called the "dereferencing operator" either, although the unary * operator *does* dereference a pointer.) –  Dec 12 '14 at 13:38
  • Thanks for the official term. I made a little edit to avoid misunderstandings. – Peter - Reinstate Monica Dec 12 '14 at 13:49
  • @Peter Schneider If p[n] is equivalent to *(p+n) then why is it showing an error if we write *p[i]. Also what do u mean by &p[n] is equivalent to p+n ? – Rooney10 Dec 12 '14 at 13:57
  • 1
    Well, `*p[i]` is as wrong as `**(p+i)`. As others say, you are trying to dereference something which is not an address (`p[i]` is a normal int, like `i`). AND: What I mean with "&p[n] is equivalent to p+n" is exactly that. The two expressions are equivalent. Perhaps it helps when I say that `p+n` is the **address** of the nth element after the element at p? And since `p[n]` is the nth element, `&p[n]` is logically the nth element's address as well. – Peter - Reinstate Monica Dec 12 '14 at 14:01
  • 1
    @PeterSchneider int arr[]={1,2,3,4,5} Now if we do int *p=arr; Then p[i] and arr[i] are basically same right? Now here we created a memory space and the pointer points to the memory space so we can use either p[i] or arr[i] for accessing an element, is that what we are saying? Now say we want the pointer address then what should we write in printf? – Rooney10 Dec 12 '14 at 14:21
  • 1
    All you say is right. "Now say we want the pointer address": Do you really want the pointer *address?* Probably you want the pointer *value*. The pointer p is a normal variable which contains a normal value, much like an int. On common platforms that value would be 4 or 8 bytes long. No magic there, just a bit pattern. You could intepret it as an int and e.g. print the number. What that number *means* is interesting: The number is the "address" of a memory location. That is what scanf needs: The address of a memory location. To change arr's first element, 1, just write `scanf("%d", p)`. – Peter - Reinstate Monica Dec 12 '14 at 14:28
  • Oh, and that would be equivalent to `scanf("%d", arr)`. – Peter - Reinstate Monica Dec 12 '14 at 14:30
1

Why do we need to write & in the scanf, if it's an array it's not necessary?

Arrays decay to a pointer of the first element. Generally an expression a, where a is an array, turns into &a[0].

Also if we write *p[i] in printf, it gives an error, p is a pointer so we should deference it and store the data in the reserved space in the memory but it's not working?

No, you're trying to apply the unary * operator to something which isn't a pointer type. An expression of the form E1[E2] is equivalent to (*((E1)+(E2))), so if whatever is yielded by * isn't a pointer type, then the syntax is invalid.

  • 1. P is pointer to array so &p should display the pointer address and not the memory address of the element? 2.Say int *p=&a; a=5; *p=8 printf("%d",*p); It should print 8 right but 8 is not a pointer but it still shows the correct answer? – Rooney10 Dec 12 '14 at 13:55
0

You need to write &n to tell scanf where to put the data. & is the address operator, it returns the memory address where the variable is stored.

&p[i] will give the address of the ith element of the array.

glglgl
  • 89,107
  • 13
  • 149
  • 217
Andreas Argelius
  • 3,604
  • 1
  • 13
  • 22
  • 1
    It is not a reference operator. There are no references in C. It isn't called the reference operator in C++ either. –  Dec 12 '14 at 13:31