Simple Program for Pointer and Array Example in C
On this page (7sections)
About this program
This is an example program in c pointer example programs. Read the concept first: C Pointers, then study the code and output below.
Overview
- Array Pointer Assignment : pointer_variable = &var[position];
- pointer_variable++ is increasing Address value based on Data Type
- pt++; —> Its locate next position of Array
for example,
Array Pointer Assignment
pt = &var[0];
pt++;
now pt++ points var[1] of address.
Syntax
Array Pointer Assignment
pointer_variable = &var[position];
pointer_variable++ is increasing Address value based on Data Type
pt++; --> Its location next position of Array
Simple Program for Pointer and Array Example
/* Simple Program for Pointer and Array Example in C*/
/* Pointer and Array Program,C Pointer Examples */
#include <stdio.h>
#define MAX_SIZE 5
int main() {
int var[] = {10, 20, 30, 40, 50};
int i = 0;
//Pointer Variable Declaration for Integer Data Type
int *pt;
//& takes the address of var , Here now pt == &var, so *pt == var
pt = &var[0];
while (i < MAX_SIZE) {
printf("Position : %d # Actual : Value : %d , Address = %p \n", i, var[i], &var[i]);
printf("Position : %d # Pointer : Value : %d , Address = %p \n\n", i, *pt, pt);
i++;
// pt++ is increasing Address value based on Data Type
pt++;
}
return 0;
}
Sample Output
Position : 0 # Actual : Value : 10 , Address = 0060FEF4
Position : 0 # Pointer : Value : 10 , Address = 0060FEF4
Position : 1 # Actual : Value : 20 , Address = 0060FEF8
Position : 1 # Pointer : Value : 20 , Address = 0060FEF8
Position : 2 # Actual : Value : 30 , Address = 0060FEFC
Position : 2 # Pointer : Value : 30 , Address = 0060FEFC
Position : 3 # Actual : Value : 40 , Address = 0060FF00
Position : 3 # Pointer : Value : 40 , Address = 0060FF00
Position : 4 # Actual : Value : 50 , Address = 0060FF04
Position : 4 # Pointer : Value : 50 , Address = 0060FF04
Related Pages
Learn the concept first, then study the code:
- C Programs — Browse all C Programs.
- C Pointers — Concept — pointers, addresses and dereferencing.
- Simple Pointer Example Program — More in c pointer example programs.
- Simple Program for Print address of Variable Using Pointer in C — More in c pointer example programs.