Skip to main content

Creating An Array of Variable Size-2

Another Method for Array of Variable Size

So, I have already introduced you to a new method to create an array of variable size but there is a drawback in that method.
(Those who have not red that method yet can have a look at it.

And the drawback is that you have to create another function to create that array and sometimes you need to enter a variable of which the array size is selected and that variable is to be entered in the middle of your program then you have to call the another function to create the array in middle of your program.And suppose if the variable in inside a loop then array is created so many times but you need only the first case.In that scenario, the previous method would not be suitable.
But for small programs that method is still valuable.
Here i am presenting you another method to do our job.
That method is based on the technique of dynamic memory allocation.Here what we do is that we create a pointer variable and then required size of memory is allocated of which our pointer points to the first place.
And then we can use it as a normal array.
Here is the sample program

main()
{
int i, *p, n;
scanf("%d",&n) ; //size of array
p=(int*)malloc(n*(sizeof(int))) ;
for(i=0 ; i<n ; i++)
p[i]=i ;

for(i=0 ; i<n ; i++)
printf("%d\n",p[i]) ;
}

for n=5
The output would be........
0
1
2
3
4

This method is totally okay.But there is still some drawback.As this method is based on dynamic memory allocation, it might be possible that during runtime, memory will not be able to be allocated.
So use it for limited array size (limits depend on the machine you are using).








Comments

Popular posts from this blog

Image Search Engine Using Python

Images provide a lot more information than audio or text. Image processing is the prime field of research for robotics as well as search engines. In this article we will explore the concept of finding similarity between digital images using python. Then we will use our program to find top 10 search results inside a dataset of images for a given picture. It won't be as good as google's search engine because of the technique we will be using to find similarity between images. But what we are going to make will be pretty cool. So lets start. Setting up the Environment Our Algorithm How the code looks Lets build the GUI Additional Techniques Setting up the Environment The code we are going to write requires a few tools which we need to install first. I will try to be as precise as i can and if you get stuck into installing some tool then you can drop a comment below and i will help you sort out the problem. So here are the tools and the steps to install ...

Cordova viewport problem solved

Include the viewport settings in Cordova If you are facing the auto zooming problem of cordova then go read on the full article. Cordova actually ignores the viewport meta tag which causes the pixel density problem. So we need to tell cordova that viewport tag is equally important as other tags. To do this, we need to add some code to a file which is specify in the article. Corodva messes with pixels If you are using the latest cordova version or creating the cordova app for latest android versions then you may have faced the zoom malfunctioning.I also faced it when creating an app. Many of you may have already searched the web and found the answer of changing the meta tag attributes to get it working. But adding target-densitydpi=medium-dpi does not solve the problem for latest android versions. It may work for gingerbread but not for kitkat and others. So the final solution which i found was one of the stackexchange answer but rarely found. So i am gonna two things here, i ...

Why Interfaces are Awesome in Go

Why Interfaces are Awesome in Go Language Go, or Golang, is known for being a simple, yet powerful programming language. One of the core concepts that makes Go stand out is interfaces . Interfaces in Go help you write flexible, reusable, and maintainable code. They allow different types to share common behaviors without worrying about the exact type behind the scenes. In this article, we'll dive into why interfaces are awesome, with easy-to-understand examples. We’ll also give some tips on how to make the best use of them in your Go programs. What is an Interface? An interface in Go defines a set of method signatures (rules), but it doesn't provide the implementation. Instead, any type that implements these methods can be said to "satisfy" the interface. This allows you to write code that can work with different types as long as they share certain behaviors. Basic Example of an Interface package main import "fmt" // Define an interface with ...