declare

Create array dynamically in C using malloc



Sharing buttons:

you

hi friends GT coding here in this

tutorial you will learn how to create

arrays dynamically so normally when we

create an array what we have to do is we

have to provide the size of the array at

the time of writing the program so if we

have a scenario where you do not know

the size of the array beforehand then we

cannot use this approach to write our

program so first of all we will look at

how we create an array normally so int

main this is a C program here first of

all we will write the data type named

int and then the name of the array for

example we will name this array a and

the size of the array in this square

brackets we can initialize this array by

providing some numbers here so now this

is our array of four elements and now we

have a situation where we do not know

the number of items in the array and we

have to create it at run time so the

first thing that comes into your mind is

is that maybe we can do this int size we

will create a variable named size and

then we will get the size from the user

at program run time and then we will

create

int a and provide size in here but this

does not work the reason is that this

here should not be a variable this

should be a constant so this is the

syntax of creating arrays so we cannot

use this technique to create arrays

dynamically so what we have to do is use

a function in C called malloc so malloc

is a function which can be used to get

memory from the heap in the main memory

so in heap we can get

any amount of memory that we want until

the main memory is itself full to use

malloc we have to first create a pointer

so first we'll create int star a this is

a pointer now we will write malloc and

here we have to write how many bytes of

memory we want so if we want to create

an array of size 5 then we need 20 bytes

of memory that is because an integer has

a size of 4 bytes and if you want 5

integers then we have to have 20 bytes

of memory if you want to know more about

datatypes and its sizes and basic

concepts of memory you can watch the

video titled computer memory basics in

this channel so here now we will pass

size x size of int so suppose if a user

inputs 5 then we get 5 x sizeof int

which is 4 so 5 x 4 is 20 so this

parameter parameter becomes 20 now

malloc returns a void pointer so we have

to first type cash this into a pointer

to integer now we have created an array

of the size provided by the user so now

we will write some more code to make

this more clearer right enter the size

of the array

and here we will get some values from

the user and to the values of T added so

we will loop through the array for I is

equal to 0 to size we will scan and put

the value in the right index of a now we

have the array initialized with the

values provided by the user now we will

print the array the values in the array

for I is equal to 0-2 size primitive and

I so this will print the items of the

array so let's check the program so here

is the program enter the size of the

array

let's put 4 and through the enter the

values of the array we will enter some

random values so here we have the values

of the array 21:56 89 and 12 so this is

how you create an array dynamically hope

you have understood this program thank

you for watching