
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Difference between pointer and array in C
The details about a pointer and array that showcase their difference are given as follows.
Pointer
A pointer is a variable that stores the address of another variable. When memory is allocated to a variable, pointer points to the memory address of the variable. Unary operator ( * ) is used to declare a pointer variable.
The following is the syntax of pointer declaration.
datatype *variable_name;
Here, the datatype is the data type of the variable like int, char, float etc. and variable_name is the name of variable given by user.
A program that demonstrates pointers is given as follows.
Example
#include <stdio.h> int main () { int a = 8; int *ptr; ptr = &a; printf("Value of variable a: %d
", a); printf("Address of variable a: %d
", ptr); return 0; }
The output of the above program is as follows.
Value of variable a: 8 Address of variable a: -2018153420
Array
An array is a collection of the same type of elements at contiguous memory locations. The lowest address in an array corresponds to the first element while highest address corresponds to the last element. Array index starts with zero(0) and ends with the size of array minus one(array size - 1).
Output
The following is the syntax of array.
type array_name[array_size ];
Here, array_name is the name given to an array and array_size is the size of the array.
A program that demonstrates arrays is given as follows.
Example
#include <stdio.h> int main () { int a[5]; int i,j; for (i = 0;i<5;i++) { a[i] = i+100; } for (j = 0;j<5;j++) { printf("Element[%d] = %d
", j, a[j] ); } return 0; }
Output
The output of the above program is as follows.
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104
- Related Articles
- Difference Between Array and Pointer
- Difference Between Pointer and Reference
- What is difference between a pointer and reference parameter in C++?
- Explain the concept of Array of Pointer and Pointer to Pointer in C programming
- Pointer vs Array in C
- Difference between Structure and Array in C
- Difference between Array and Pointers in C
- Pointer to an Array in C
- C program to display relation between pointer to pointer
- Difference between std::vector and std::array in C++
- Double Pointer (Pointer to Pointer) in C
- Sum of array using pointer arithmetic in C++
- Sum of array using pointer arithmetic in C
- Explain the concept of pointer to pointer and void pointer in C language?
- Difference between Array and ArrayList
