top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What is the difference between array and pointer in C?

+2 votes
361 views
What is the difference between array and pointer in C?
posted Dec 8, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+3 votes

Pointer
1. A pointer is a place in memory that keeps address of another place inside.
2. Pointer can’t be initialized at definition.
3. Pointer is dynamic in nature. The memory allocation can be resized or freed later.
4. The assembly code of Pointer is different than Array.

Array
1. An array is a single, pre allocated chunk of contiguous elements (all of the same type), fixed in size and location.
2. Array can be initialized at definition. For Example
int num[] = { 2, 4, 5}
3. They are static in nature. Once memory is allocated , it cannot be resized or freed dynamically.
4. The assembly code of Array is different than Pointer.

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, see the following programs to justify this statement.

Behavior of sizeof operator

// 1st program to show that array and pointers are different

#include <stdio.h>
int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   int *ptr = arr;

   // sizof(int) * (number of element in arr[]) is printed
   printf("Size of arr[] %d\n", sizeof(arr));

   // sizeof a pointer is printed which is same for all type 
   // of pointers (char *, void *, etc)
   printf("Size of ptr %d", sizeof(ptr));
   return 0;
}

Output:

Size of arr[] 24
Size of ptr 4

Although array and pointer are different things, following properties of array make them look similar.

1) Array name gives address of first element of array.
Consider the following program for example.

#include <stdio.h>
int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   int *ptr = arr;  // Assigns address of array to ptr
   printf("Value of first element is %d", *ptr)
   return 0;
}

Output:

Value of first element is 10

2) Array members are accessed using pointer arithmetic.
Compiler uses pointer arithmetic to access array element. For example, an expression like “arr[i]” is treated as *(arr + i) by the compiler. That is why the expressions like *(arr + i) work for array arr, and expressions like ptr[i] also work for pointer ptr.

#include <stdio.h>
int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   int *ptr = arr;
   printf("arr[2] = %d\n", arr[2]);
   printf("*(ptr + 2) = %d\n", *(arr + 2));
   printf("ptr[2] = %d\n", ptr[2]);
   printf("*(ptr + 2) = %d\n", *(ptr + 2));
   return 0;
}

Output:

arr[2] = 30
*(ptr + 2) = 30
ptr[2] = 30
*(ptr + 2) = 30 

3) Array parameters are always passed as pointers, even when we use square brackets.

#include <stdio.h>

int fun(int ptr[])
{
   int x = 10;

   // size of a pointer is printed
   printf("sizeof(ptr) = %d\n", sizeof(ptr));

   // This allowed because ptr is a pointer, not array
   ptr = &x;

   printf("*ptr = %d ", *ptr);

   return 0;
}
int main()
{
   int arr[] = {10, 20, 30, 40, 50, 60};
   fun(arr);
   return 0;
}

Output:

sizeof(ptr) = 4
*ptr = 10
answer Dec 8, 2015 by Shivaranjini
One more point is arrayname is a constant where as pointer is a variable. For example
int a[10];
int *p=a;
p++; // Valid statement
a++; // invalid as a is constant hence we cant change a
+1 vote

Difference of Pointer and Array in C/C++

  1. A pointer is a place in memory that keeps address of another place inside
  2. An array is a single, pre allocated chunk of contiguous elements (all of the same type), fixed in size and location.
  3. Pointer can’t be initialized at definition.
  4. Array can be initialized at definition. Example

    int num[] = { 1, 2, 3,4,5}

  5. Pointer is dynamic in nature. The memory allocation can be resized or freed later.

  6. They are static in nature. Once memory is allocated , it cannot be resized or freed dynamically.
  7. The assembly code of Pointer is different than Array
  8. The assembly code of Array is different than Pointer.
answer Dec 10, 2015 by Rajan Paswan
Similar Questions
+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

+1 vote

Can someone help me with the example when to use normal function calling and when pointer function calling. What was the reason for introducing pointer function calling in C?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...