Sign in to follow this  

Creating a Dynamic Array Function

This topic is 1501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to make a function in C that reads an integer N from the user and then creates a dynamic array with N integer positions.
 
I had problems with my parameters when I tried. Specifically the array parameters.
 
This is how I create the dynamic array without creating the function.


#include <stdio.h>
#include <stdlib.h>



int main()
{
      int *array,N,i;
      scanf("%d",&N);
      array = (int*)malloc(N*sizeof(int));
      for (i = 0; i<N; i++)
      {
           scanf("%d",&array[i]);
      }

       return 0;
}

Thanks in advance.

Edited by shadowstep00

Share this post


Link to post
Share on other sites

The code looks broadly correct to me, though it lacks error handling. Can you explain the nature of these problems in more detail?

Share this post


Link to post
Share on other sites

The code I posted has no problem. 

 

I want this code to be modified.

I want to create a function which takes an integer N (given by the user) and returns a dynamic array with N positions.

Edited by shadowstep00

Share this post


Link to post
Share on other sites
Do you mean this seriously?
You need just a new function with one parameter
And the return type of your array in your case a pointer to int.
What is the difficulty?

Share this post


Link to post
Share on other sites

So.. 

 

I just do this then.

 

#include <stdio.h>
#include <stdlib.h>
 
int create_array(int N);
 
int main()
{
       int *a,N,i;
       scanf("%d",&N);
       a = create_array(N);
 
       for (i = 0; i<N; i++)
      {
          scanf("%d",&a[i]);
      }
 
      return 0;
}
 
int create_array(int N)
{
         return (int*)malloc(N*sizeof(int));
}


Program runs ok. But I get these 2 warning
 
In function 'main':
assignment makes pointer from integer without a cast [enabled by default]
 
In function 'create_array':
return makes integer from pointer without a cast [enabled by default]
 
Edited by shadowstep00

Share this post


Link to post
Share on other sites

Your function should return an int pointer, not an int:

int *create_array(int N)

You'll need to change the declaration and definition.

 

The compiler is warning that you're trying to treat an integer as a pointer and vice versa. You should probably set your compiler to treat warnings as errors if that only generates a warning, it usually indicates a serious mistake.

 

Also note that the use of capitals is usually reserved for constants.

 

Finally, you should free() pointers returned from malloc() when you are finished with them. Otherwise, in bigger programs you will be creating memory leaks.

Edited by rip-off

Share this post


Link to post
Share on other sites

Thanks for your help. :D 

 

Just got to dynamic arrays and did not yet anything about pointer functions.

 

Problem Solved.

Share this post


Link to post
Share on other sites
Sign in to follow this