C Pointers with Arrays - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about pointers with one-dimensional and two-dimensional arrays.
An array variable name itself represents a pointer to the first element of that array.
In the statement int num[10];, the address of the first array element can be expressed as either &num[0] or num. Similarly, the address of the second array element can be written as &num[1] or as num +1, and so on.
In general, the address of the ith array element can be expressed as either &num[i] or (num +i).
Thus, the address of an array element can be expressed in two ways: either by writing the actual array element proceeded by the ampersand or by writing an expression in which the subscript is added to the array variable name.
The expressions &num[i] and (num + i) both represent the address of the ith element of num, and num[i] and *(num +i) both represent the value at that address.
The address of the given array element can be found by using the formula:
Address of num[i] = base address + i * scale factor // scale factor is the size of the data type used to create an array
By using heap memory, A programmer can create a new one-dimensional array at the run time and it can be accessed by using the pointers.
To create heap memory, the programmer can use either malloc() or calloc().
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
     int *p, n, i;
     printf("Enter n value : ");
     scanf("%d", &n);
     p = (int *) malloc(n * sizeof(int));
     printf("Enter %d values : ", n);
     for (i = 0; i < n; i++) {
         scanf("%d", p + i);
     }
     printf("The given array elements are : ");
     for (i = 0; i < n; i++) {
         printf("%d ", *(p + i));
     }
}
Output:
Enter n value : 10                                                                                                                              
Enter 10 values : 1                                                                                                                             
2                                                                                                                                               
3                                                                                                                                               
4                                                                                                                                               
5                                                                                                                                               
6                                                                                                                                               
7                                                                                                                                               
8                                                                                                                                               
5                                                                                                                                               
2                                                                                                                                               
The given array elements are : 1 2 3 4 5 6 7 8 5 2
 
Like a normal variable, a pointer variable can also be passed as an argument to the function.
A pointer variable can send the address to the formal parameter, on the other side, the formal parameter can also be declared as the same as the pointer variable (like call by address).
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void read(int *, int);
void display(int *, int);
void main() {
     int *p, n, i;
     printf("Enter n value : ");
     scanf("%d", &n);
     p = (int *) malloc(n*sizeof(int));
     read(p, n);
     printf("The given elements are : ");
     display(p, n);
     printf("\n");
}
void read(int *a, int n) {
     int i;
     printf("Enter %d values : ", n);
     for (i = 0; i < n; i++) {
          scanf("%d", a + i);
     }
}
void display(int *a, int n) {
     int i;
     for (i = 0; i < n; i++) {
         printf("%d ", *(a + i));
     }
}
Output:
Enter n value : 4                                                                                                                             
Enter 4 values : 12                                                                                                                           
55                                                                                                                                                                                                                                                                         
74                                                                                                                                            
53                                                                                                                                            
The given elements are : 12 55 74 53
We know that an array variable name itself represents a pointer to the first element of that array.
In the statement int num[5][5];, the address of the first row first column element can be expressed as either &num[0][0] or num. Similarly, the address of the first row second column element can be written as &num[0][1] or (num + 0 * 5 + 1), and so on.
The address of the given array element can be found by using the formula:
Address of num[i][j] = base address + i * total number of columns * scale factor + j * scale factor 
// scale factor is the size of the data type of the array
Using heap memory, a programmer can create a new two-dimensional array during the run-time which can be accessed using pointers.
To create heap memory, a programmer can use either malloc() or calloc() function.
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
     int *p, m, n, i, j;
     printf("Enter row and column size : ");
     scanf("%d %d", &m, &n);
     p = (int*)calloc(m*n, sizeof(int));
     printf("Enter %d matrix elements : ", m*n);
     for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
             scanf("%d", p + i * n + j);
         }
     } 
     printf("The given matrix is \n");
     for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
             printf("%d ", *(p + i * n + j));
         }
         printf("\n");
     }   
     free(p);
}
Output:
Enter row and column size : 2                                                                                                                 
2                                                                                                                                             
Enter 4 matrix elements : 4                                                                                                                   
5                                                                                                                                             
2                                                                                                                                             
3                                                                                                                                             
The given matrix is                                                                                                                           
4 5                                                                                                                                           
2 3
Whenever addresses are stored as array elements, such an array is called an array of pointers.
The format for the declaration of an array of pointers is
data_type *array_name [size];
	(or)
data_type *(array_name [size]);
The following declaration creates an array named ptr_array containing 5 elements, each of which is of type int *.
int *ptr_array[5];
The above declaration creates an array of pointers with size 5, named as ptr_array. In each location, the user needs to store the addresses of another variable of the same data type int.
Let’s consider an example:
#include <stdio.h>
#include <stdlib.h>
void main() {
     float *a[5], f1, f2;
     f1 = 10.17;
     f2 = 35.10;
     a[0] = &f1;
     a[1] = &f2;
     printf("First Value = %f\n", *(a[0]);
     printf("Second Value = %f\n", *(a[1]);
}
Output:
First Value = 10.170000                                                                                                                       
Second Value = 35.099998
A multidimensional array can be expressed in terms of an array of pointers rather than as a pointer to a group of contiguous arrays.
In such cases, the newly defined array will have one less dimension than the original array. Each pointer will indicate the beginning of a separate (n – 1)th dimensional array.
In general, a two-dimensional array can be defined as a one-dimensional array of pointers as Data_type *array_name [size1]; rather than Data_type array_name[size1][size2];.
Similarly, an n-dimensional array can be defined as an (n – 1)th dimensional array of pointers as Data_type *array_name[size1] [size2] ———- [sizen-1]; instead of Data_type array_name[size1] [size2][size3]———–[sizen];.
Here the preceding asterisk (*) establishes that the array will contain pointers.
Consider the following example:
int *num[10];
Here num[0] points to the beginning of the first row, num[1] points to the beginning of the second row, and so on. In this kind of definition, the number of elements in each row is specified at the run time.
An individual array element, such as array[2][4] can be accessed by *(array[2] + 4). In this expression, array[2] is a pointer to the first element in row 2, so that (ary[2] + 4) points to element 4 (actually the fifth element).
The heap memory is allocated individually for each pointer of the array in an array of pointers.
#include <stdio.h>
#include <stdlib.h>
void main() {
     int *p[10], n, m, i, j;
     printf("Enter row and column sizes : ");
     scanf("%d %d", &m, &n);
     for (i = 0; i < m; i++) {
          p[i] = (int *) malloc(n * sizeof(int));
     }
     printf("Enter %d elements : ", n*m);
     for (i = 0; i < m; i++) {
          for (j = 0; j < n; j++) {
              scanf("%d", p[i] + j);
          }
     }
     printf("The given matrix is \n");
     for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
              printf("%d ", *(p[i] + j));
         }
     printf("\n");
     }
}
Output:
Enter row and column sizes : 2                                                                                                                  
2                                                                                                                                               
Enter 4 elements : 1                                                                                                                            
2                                                                                                                                               
3                                                                                                                                               
5                                                                                                                                               
The given matrix is                                                                                                                             
1 2                                                                                                                                             
3 5
Wiki – Pointers in C
C Pointers pass by value and reference
Arrays in C
Happy Learning 🙂

C How to Pass Arrays to Functions
Basics of C Pointers | Call by value | Call by address
C – One Dimensional Arrays
C Program – Bubble Sort Program in C
C Multi Dimensional Arrays
Arithmetic Operations on NumPy Arrays
C Program – Sum of digits of given number till single digit
Dynamic Memory Allocation in C
C Program to add elements of an array
C Program – Find the factorial of given number
Java Program to swap two arrays Example
Indexing in NumPy Arrays
C Program – Check a number is Palindrome or not
C Program – Armstrong numbers between given numbers
C – Categories of Functions

C How to Pass Arrays to Functions
Basics of C Pointers | Call by value | Call by address
C – One Dimensional Arrays
C Program – Bubble Sort Program in C
C Multi Dimensional Arrays
Arithmetic Operations on NumPy Arrays
C Program – Sum of digits of given number till single digit
Dynamic Memory Allocation in C
C Program to add elements of an array
C Program – Find the factorial of given number
Java Program to swap two arrays Example
Indexing in NumPy Arrays
C Program – Check a number is Palindrome or not
C Program – Armstrong numbers between given numbers
C – Categories of Functions
 


Δ
 C – Introduction
 C – Features
 C – Variables & Keywords
 C – Program Structure
 C – Comment Lines  & Tokens
 C – Number System
 C – Local and Global Variables
 C – Scope & Lifetime of Variables
 C – Data Types
 C – Integer Data Types
 C – Floating Data Types
 C – Derived, Defined Data Types
 C – Type Conversions
 C – Arithmetic Operators
 C – Bitwise Operators
 C – Logical Operators
 C –  Comma and sizeof Operators
 C – Operator Precedence and Associativity
 C – Relational Operators
 C Flow Control – if, if-else, nested if-else, if-else-if
 C – Switch Case
 C Iterative – for, while, dowhile loops
 C Unconditional – break, continue, goto statements
 C – Expressions and Statements
 C – Header Files & Preprocessor Directives
 C – One Dimensional Arrays
 C – Multi Dimensional Arrays
 C – Pointers Basics
 C – Pointers with Arrays
 C – Functions
 C – How to Pass Arrays to Functions
 C – Categories of Functions
 C – User defined Functions
 C – Formal and Actual Arguments
 C – Recursion functions
 C – Structures Part -1
 C – Structures Part -2
 C – Unions
 C – File Handling
 C – File Operations
 C – Dynamic Memory Allocation
 C Program – Fibonacci Series
 C Program – Prime or Not
 C Program – Factorial of Number
 C Program – Even or Odd
 C Program – Sum of digits till Single Digit
 C Program – Sum of digits
 C Program – Reverse of a number
 C Program – Armstrong Numbers
 C Program – Print prime Numbers
 C Program – GCD of two Numbers
 C Program – Number Palindrome or Not
 C Program – Find Largest and Smallest number in an Array
 C Program – Add elements of an Array
 C Program – Addition of Matrices
 C Program – Multiplication of Matrices
 C Program – Reverse of an Array
 C Program – Bubble Sort
 C Program – Add and Sub without using + –