Part 5, C Arrays & Pointers
Part 5, C Arrays & Pointers
1
C ARRAYS
Array is a data structure in the C programming, which can store a fixed-size sequential
collection of elements of the same data type.
For example, if you want to store ten numbers, it is easier to define an array of 10 lengths,
instead of defining ten variables.
Table of Contents
# Define an Array in C
# Initialize an Array in C
# A Pictorial Representation of the Array:
# Accessing Array Elements in C
2
Define an Array in C
Syntax:
type arrayName [ size ];
This is called a one-dimensional array. An array type can be any valid C data types, and array
size must be an integer constant greater than zero.
Example:
double amount[5];
3
Initialize an Array in C
5
Accessing Array Elements in C
Example:
int myArray[5];
int n = 0;
6
C Strings
Table of Contents
# Strings Declaration in C
# Strings Initialization in C
# Memory Representation of Above Defined String in C
Strings Declaration in C
char name[6];
Through pointers.
char *name; 7
Strings Initialization in C
Example:
or
8
Example:
#include<stdio.h>
int main ()
{
char name[6] = {'C', 'l', 'o', 'u', 'd', '\0'};
printf("%s\n", name );
return 0;
}
9
C Pointers
A pointer is a variable in C, and the pointers value is the address of a memory location
in your RAM.
Table of Contents
# Pointer Definition in C
# Benefits of using Pointers in C
# How to Use Pointers in C
10
Pointer Definition in C
Syntax:
type *variable_name;
Example:
int *width;
char *letter;
#include<stdio.h>
int main ()
{
int n = 20, *ptr; //actual and pointer variable declaration
ptr = &n; //store address of n in pointer variable
printf("Address of n variable: %x\n", &n );
printf("Address stored in ptr variable: %X\n", ptr ); //address stored in pointer variable
printf("Value of *ptr variable: %d\n", *ptr ); //access the value using the pointer
return 0;
}
12
C Memory Management
C language provides many functions that come in header files to deal with the allocation and
management of memories. In this tutorial, you will find brief information about managing
memory in your program using some functions and their respective header files.
Table of Contents
# Management of Memory
# static memory allocations
# dynamic memory allocations
Management of Memory
Almost all computer languages can handle system memory. All the variables used in your
program occupies a precise memory space along with the program itself, which needs some
memory for storing itself (i.e., its own program). Therefore, in managing memory, utmost care
is one of the major tasks a programmer must keep in mind while writing codes.
13
When a variable gets assigned in a memory in one program, that memory location cannot be
used by another variable or another program. So, C language gives us a technique of allocating
memory to different variables and programs.
14
C Dynamic Memory Allocation
malloc, calloc, or realloc are the three functions used to manipulate memory. These commonly
used functions are available through the stdlib library so you must include this library to use
them.
Table of Contents
# C - Dynamic memory allocation functions
# malloc function
# Example program for malloc() in C
# calloc function
# Example program for calloc() in C
# realloc function
# free function
# Example program for realloc() and free()
15
C - Dynamic memory allocation functions
Function Syntax
malloc() malloc (size); //memoryallocation
calloc() calloc (number_of_units, unit_size); //contiguous allocation
realloc() realloc (pointer_name, new_size); //re-allocation
free() free (pointer_name);
16
Example program for malloc() in C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *allocated;
char *school = “Federal University of Agriculture, Abeokuta“;
int length = strlen(school);
allocated = (char *) malloc( length * sizeof(char) + 1 ); //memory allocated dynamically, +1 to accommodate the trailing ‘\0’
if(allocated == NULL )
{
printf("Couldn't allocate requested memory\n");
return;
}
else
{
strcpy(allocated, school);
}
printf("Dynamically allocated memory content: %s", allocated );
free(allocated);
}
Program Output:
Dynamically allocated memory content: Federal University of Agriculture, Abeokuta
17
calloc function
calloc() function and malloc () function are similar. But calloc() allocated memory is ascii zero-initialized. However, malloc() does not perform any
initialization.
Example:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *school = “Federal University of Agriculture, Abeokuta“;
int size = strlen (school) + 1;
char *allocated = (char *) calloc(size, sizeof(char)); //memory allocated dynamically
if(allocated == NULL )
{
printf("Couldn't allocate requested memory\n");
return;
}
else
{
strcpy(allocated, school);
}
printf("Dynamically allocated memory content: %s", allocated);
free(allocated);
}
Program Output:
18
realloc function
realloc function modifies the allocated memory size by malloc and calloc functions to
new size.
If enough space doesn't exist in the memory of the current block to extend, a new block
is allocated for the full size of reallocation, then copies the existing data to the new block
and then frees the old block.
free function
free function frees the allocated memory by malloc (), calloc (), realloc () functions.
19
Example program for realloc() and free()
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *school = “Federal University of Agriculture, Abeokuta“;
int size = strlen (school) + 1;
char *allocated = (char *) malloc(size, sizeof(char)); //memory allocated dynamically
if(allocated == NULL )
{
printf("Couldn't allocate requested memory\n");
return;
}
else
{
strcpy(allocated, school);
}
20
Contd: realloc() and free()
if(allocated == NULL )
{
printf("Couldn't allocate requested memory\n");
return;
}
else
{
strcpy(allocated, "space is extended up to 100 characters");
}
Program Output:
Exercise:
Is a cast necessary for realloc()?
Why?
21
Thank you for your attention
22