C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Pointer and Arrays

In C Programming pointers and arrays are very closely related to each other in terms of functionality. Both pointers as well as arrays uses consecutive memory locations to store the data with one key difference in accessing the data. Pointer uses address to access the data stored in a memory location whereas array uses index value to access the data stored in a memory location. Fortunately data or strings initialized using the pointer variable can be accessed using array and vice versa.

pointer array in c

Program - Pointers To Initialize, Arrays To Access

pointer-array.c
#include <stdio.h>
int main()
{
char *ptr ="pointer array", i;
for(i=0; i<13; i++)
printf("%c",ptr[i]);
return 0;
}
  • pointer array

Note:

In the above example program *ptr is a char pointer variable which is initialized by a string then the same pointer variable is used as an array to access the value get stored by a pointer variable *ptr.

C Program - Arrays To Initialize, Pointers To Access

array-pointer-access.c
#include <stdio.h>
int main()
{
char arr[13] = "Pointer array", i, *ptr;
ptr = arr;
for(i =0; i<13; i++)
printf("%c",*ptr++);
return 0;
}
  • pointer array

Note:

In the above example program arr[13] is a char array variable which is initialized by a set of characters then the address of first element in an array is assigned to the pointer variable which is then post incremented to print all the characters initialized to the array variable arr[13].

Ways To Expressions Pointer Array Elements

There are four different syntaxes are used to express the Pointer and array elements. Two syntaxes for array and two syntaxes for pointer.

  • a[i]
  • i[a]
  • *(a + i)
  • *(i + a)

C Program - Access Pointer Array Elements

pointer-array-access.c
#include <stdio.h>
int main()
{
int i, a[3] = {10, 11, 12};
for(i = 0; i < 3; i++)
{
printf("%d \t", a[i]);
printf("%d \t", i[a]);
printf("%d \t", *(a + i));
printf("%d \n", *(i + a));
}
return 0;
}
  • 10      10      10      10
  • 11      11      11      11
  • 12      12      12      12

Note:

In the above example program array can express their elements either a[i] or i[a]. If array can express their elements in two ways then pointers too can express their elements in two ways (*(a + i) or *(i + a)) as pointers and arrays are all a same family.

Pointers And Two Dimensional Arrays

The general form of two dimensional array looks arr[i] [j], i represents the number of rows in an array whereas j represents the number of column in an array. The base address of an array is arr[0][0].

C Program - Pointers And Two Dimensional Arrays

pointers-2d-array.c
#include <stdio.h>
int main()
{
int *iptr;
int i, a[2][2] = {10, 11, 12, 13};
iptr = &a[0][0];
for(i = 0; i < 4; i++)
{
if(i == 2)
{
printf("\n");
}
printf("%d ", *(iptr+i));
}
return 0;
}
  • 10 11
  • 12 13

Note:

In the above example program two dimensional array is declared and initialized. Though it is a two dimensional array, the elements get stored in the consecutive memory locations. Thus with pointers capability we access all the elements which we initialized to the two dimensional array earlier.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report