Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Creating a Dynamic Array Function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 shadowstep00   Members   -  Reputation: 527

Like
0Likes
Like

Posted 08 December 2013 - 05:51 AM

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, 08 December 2013 - 06:18 AM.

Failure is not an option...


Sponsor:

#2 rip-off   Moderators   -  Reputation: 8516

Like
0Likes
Like

Posted 08 December 2013 - 06:07 AM

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



#3 shadowstep00   Members   -  Reputation: 527

Like
0Likes
Like

Posted 08 December 2013 - 06:18 AM

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, 08 December 2013 - 06:19 AM.

Failure is not an option...


#4 exOfde   Members   -  Reputation: 809

Like
0Likes
Like

Posted 08 December 2013 - 06:25 AM

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?

#5 shadowstep00   Members   -  Reputation: 527

Like
0Likes
Like

Posted 08 December 2013 - 06:32 AM

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, 08 December 2013 - 06:40 AM.

Failure is not an option...


#6 rip-off   Moderators   -  Reputation: 8516

Like
2Likes
Like

Posted 08 December 2013 - 06:44 AM

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, 08 December 2013 - 06:46 AM.


#7 shadowstep00   Members   -  Reputation: 527

Like
0Likes
Like

Posted 08 December 2013 - 06:52 AM

Thanks for your help. :D 

 

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

 

Problem Solved.


Failure is not an option...





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS