-2

I know this questions sounds odd but can you please say some operations we can't do with pointers but with arrays and other?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Techky
  • 85
  • 2
  • 9

2 Answers2

2

You can not use sizeof sensefully

int arr[6] = {0};
int *p = &arr[0];

sizeof arr / sizeof arr[0] gives 6
sizeof p / sizeof p[0] may give 0, 1, 2 etc (1 on my 32 bit system)

If array is member of a struct, assignation operator (=) will (deep) copy value. But for pointer, only pointer (shallow) is copied.

struct str
{
  char name[100];
  char *city;
}a, b;
...
a = b;
a.name[0] = '\0'; // b.name[0] does not change
a.city[0] = '\0'; // b.city[0] changed
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Can you please give brief explanation about deep and shallow copy? or refer me some link? – Techky Dec 04 '14 at 06:45
  • [This post](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) covers very good explanation of deep copy and shallow copy. – Mohit Jain Dec 04 '14 at 06:58
  • Quibble: You can get sizeof(), but only of the pointer itself, not of the array. But yes, this is probably what they're looking for. – keshlam Dec 04 '14 at 14:19
  • Actually, looking at it from that angle, realloc() and free() could be considered another difference... but that's misleading, because not all pointers can be safely free()d/realloc()ed either, only those at the start of the malloc()ed memory block. – keshlam Dec 04 '14 at 14:25
0

Pointers are used for storing address of dynamically allocated arrays and for arrays which are passed as arguments to functions. In other contexts, arrays and pointer are two different things,

for more you can take a look of this url http://www.geeksforgeeks.org/difference-pointer-array-c/

Kandy
  • 673
  • 9
  • 21
  • I don't quite agree with the conclusion there. The *only* differences I can think of between array names and (valid) pointers are whether the space is allocated from heap or stack , and what sizeof() reports for them. They're more similar than different. – keshlam Dec 04 '14 at 14:26