-3

I need the following quote especially the bold text , to be justified.

... This would tend to imply that somehow source[i] is the same as *(p+i).

In fact, this is true, i.e wherever one writes a[i] it can be replaced with *(a + i) without any problems.

In fact, the compiler will create the same code in either case. Thus we see that pointer arithmetic is the same thing as array indexing. Either syntax produces the same result. This is NOT saying that pointers and arrays are the same thing, they are not. We are only saying that to identify a given element of an array we have the choice of two syntaxes, one using array indexing and the other using pointer arithmetic, which yield identical results.

This is quoted from the pdf

A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.2 (PDF Version) Sept. 2003 P.No : 19

nmxprime
  • 1,506
  • 3
  • 25
  • 52

4 Answers4

3

The expressions a[i] and *(a + i) are the same by definition. The C and C++ standards define the expression syntax a[i] to be always equivalent to *(a + i), to the point where a[i] and i[a] are interchangeable.

That does not tell you anything about the relationship between pointers and arrays, because, technically, in both a[i] and *(a + i), a is an expression of pointer type. The [] operator takes a pointer expression on the left, not an array expression; and the + operator only operates on a pointer and an integer -- not an array and an integer.

That you can use an array in place of a is simply a result of the fact that an expression of array type may be implicitly converted to an rvalue expression of pointer type.

newacct
  • 119,665
  • 29
  • 163
  • 224
1

Array of type T may behave similar to pointer of type T, but their types are different and this compiler treat these as different types.


Let's understand with some pragmatic examples.

Example 1 where you can see the difference.

int a[1200] = {0};
int *p = a;
assert(sizeof a != sizeof p);

Example 2 where you can see the difference.

/* test.c */
#include<stdio.h>
#include"head.h"
struct token id_tokens[10];
int main()
{
  printf("In original file: %p",id_tokens);
  testing();
}

/* head.h */
struct token {
  int temp;
};

/* test1.c with v1 */
#include<stdio.h>
#include"head.h"
extern struct token* id_tokens;
void testing () {
  printf("In other file %p",id_tokens);
}

/* test1.c with v2 */
#include<stdio.h>
#include"head.h"
extern struct token id_tokens[];
void testing () {
  printf("In other file %p",id_tokens);
}

Output with v1: Output : In original file: 0x601040In other file (nil)
Output with v2: Output : In original file: 0x601040In other file 0x601040

Example 2 picked from extern declaration, T* v/s T[]


Further readings on array and pointer: C-faq

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

An array is a block of consecutive elements, each of the same type. For example, a block of 200 integers.

A pointer is a small object that can identify a memory location.

These are nothing like each other. The array could be gigabytes in size but the pointer is 8 bytes at most (on popular systems).

Saying "arrays and pointers are the same" or "an array is a constant pointer" or other such garbage makes as much sense as saying that a house is an envelope.


All of that has nothing to do with talking about a[n] versus *(a + n). That is a feature of the language syntax, which is a different topic to the layout of variables in memory.

We could ban the use of [] outside of declarators and C would still have the same functionality, but the code would be harder to read. The rules of the language say that you can write a[n] as a more readable version of *(a + n); and they have the same effect.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Pointers and array names are different

Yes.

Consider the below code snippet to get the difference

int a[3];

int *p = a;

a++; /* This is a error */
p++;

printf("%d\n",*p);

Array name can't be a modifiable lvalue.(According to the standard)

Where as a pointer can be.

Adding another example where you can see a difference

printf("%d\n",sizeof(a)/sizeof(*a));
printf("%d\n",sizeof(p)/sizeof(*p));

Number of elements in a array can be given by the first line but not by the second one. Why? Array name and Pointers are different

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • If array name is a constant pointer, as others say, would that mean array and const pointers are same still arrays and pointers are not same ? – nmxprime Jan 19 '15 at 06:26
  • 1
    `If array name is a constant pointer` I don't get this? Array name and pointers are different . Array decays to a pointer and hence pointer arthmetic can be used to access a element – Gopi Jan 19 '15 at 06:30
  • `If array name is a constant pointer` means, we usually say array name is a const pointer – nmxprime Jan 19 '15 at 06:36
  • But, be it array or normal variable, in assembly level, if we mention a number (such as 256 in decimal or 0xFF in hex) as lvalue, we write into that memory location, else if its a rvalue we put that number(such as 256 in decimal or 0xFF in hex) into some memory location. Modifiable lvalue (also modifiable rvalue) is an interpretation, conceptualization and agreement. After all the `const` attribute is simply checked at compile time. Why can't we have a compiler that ignores `const` checking?, [Can you please look into the question from this context ?] – nmxprime Jan 19 '15 at 06:45
  • @MattMcNabb Where have I said Arrays are const pointers. I always said array name and pointers are two different things and array name is similar to const variable which is not modifiable – Gopi Jan 19 '15 at 07:43
  • @Gopi nmxprime said it – M.M Jan 19 '15 at 07:59
  • @MattMcNabb Ok but since I didn't see the comment being addressed I felt you were addressing me . – Gopi Jan 19 '15 at 08:00