46031

How to declare a dynamic integer array in ANSI - C using malloc and put input integers into it?

Question:

First I want the user to input what is the size of the desired array. So I am using:

int size; scanf("&d",&size);

Now I want to create an integer array using a pointer and the malloc function. This is what I did:

int *p1 = (int*)malloc(sizeof(int)*size);

According to my understanding, this is like using:

int p1[size];

But how do I use it like an array?

<strong>Question</strong> 1: Now I want the user to input as many integers as he wrote into this "array". But I can't use p[0] because it is not an array, it is a pointer.

<strong>Question</strong> 2: I want to "send" this array to a function that gets an array of integers. So again, this is not an array, how can I "give" it to the function?

Answer1:

<strong>Answer to first question:</strong>

for(i = 0; i < size; i++ ) { scanf("%d",&p[i]); /*p[i] is the content of element at index i and &p[i] is the address of element at index i */ }

Or

for(i = 0; i < size; i++ ) { scanf("%d",(p+i)); //here p+i is the address of element at index i }

<strong>Answer to second question:</strong>

For sending this array to the function, just call the function like this:

function(p); //this is sending the address of first index of p void function( int *p ) //prototype of the function

Answer2:

<ul><li>Question 1: 1d arrays and pointers to properly allocated memory are pretty-much the same thing.</li> <li>Question 2: When passing an array to a method you are actually passing the address of the 1st element of that array</li> </ul>

<em>An array is actually a pointer to the first element of the array</em>

Answer3:

You can use the subscript-syntax to access an element of your pointer.

p1[3] = 5; // assign 5 to the 4th element

But, this syntax is actually converted into the following

*(p1+3) = 5; // pointer-syntax

For your second question, define a function and pass a pointer

int* getarray(int* p1, size_t arraysize){ } //define the function int* values = getarray(p1, size); // use the function

Answer4:

sorry for bothering everyone but Miss Upasana was right this is the correct method for dynamic array use. After declaring ur array through malloc u can directly use it exactly as array as follows::

for(int i = 0; i < size; i++ ) { scanf("%d",p+i); /*p+i denoting address of memory allocated by malloc */ }

Second Answer: Now simply pass this address to any function use address to find values like:

function(int *p) /* access as &p for first value &p+2 for second p+4 for third and so on*/

Recommend

  • How to Cache Images Dynamically-Generated with PHP?
  • passing parameter to server in ExtJs
  • How to Divide an array on c#?
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • retrieving data from url in iphone
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • .NET video play library which allows to change the playback rate?
  • Detecting null parameter in preprocessor macro
  • 550 Access denied - Invalid HELO name
  • converting text file into xml using php?
  • ActiveRecord query for a count of new users by day
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Linq Objects Group By & Sum
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Optimizing database types to compact database (SQLite)
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Updating server-side rendering client-side
  • Javascript convert timezone issue
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Jquery - Jquery Wysiwyg return html as a string
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to pass list parameters for each object using Spring MVC?
  • Arrays break string types in Julia
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Java static initializers and reflection
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal