The problem is that the line with "atoi" can not be compiled. If I try to debug my code, this statement comes out "Unhandled exception at 0x7C17F2F6 (ucrtbased.dll) in A5(Dynamic Memory allocation).exe: An invalid parameter was passed to a function that considers invalid parameters fatal."
I think I didn't missed any single thing for the atoi part. I am using visual studio, and would that be the problem? Shall I change the program??
This is the code:
#include <stdio.h>
#include <stdlib.h>
double arith_seq(double*, int, int);
int main(int argc, char* argv[])
{
double* in;
int num;
num = atoi(argv[1]);
in = (double*)malloc((num+1) * sizeof(double));
if (in == NULL)
{
num = 1;
arith_seq(&in, num, 0.1);
}
//if there is no input
else
{
in[0] = 0;
arith_seq(&in, num, 0.1);
}
//in normal case
printf("My last array element is : %f", in[num - 1]);
//print the last element of the array
free(in);
return 0;
}
double arith_seq(double* parr, int size, int com)
{
for (int i = 1; i < size; i++)
{
parr[i] += parr[i - 1] + com;
}
return *parr;
}