Declare an array of variable length

Declare an array of variable length in C language

A lot of people keep asking about an array of variable length.Then they find a more often answer in the books, on the net i.e. use dynamic memory allocation.Though it is very important concept because it uses the memory in run time which reduces the execution time of your program but so many people are not used to this method which makes them fuzzy.I know another method to declare an array of variable length which is very comfortable to use.


There you go....
Firstly create a function which will recieve the input from the user which is the length of your array.In most cases it is done in the main function.So how it looks...

main()
{ int n ;
scanf("%d", &n) ;
func(n) ;
}


Then create a function to which you'll send the length variable as an arguement.In our case it in func()


Now the final step, declare the array with the previous variable as its length.
func(int k)
{ int arr[k] ;
.....your code.....
}


I hope you like the method, its simple and handy.You'll get never confused by using it.Still if want to do the same thing using malloc() then read our Variable array using malloc article.

Comments

Popular posts from this blog

Search box for blog or website

Image Search Engine Using Python

Cordova viewport problem solved