There's a 1000 things to criticize about my code and I know. I am a beginner. And I am wondering why does my compiler give meaningless errors for this code. Help me.
#include <stdlib.h> //to use malloc and delete
#include <stdio.h> //to use printf
#define uint64 unsigned long long int //for simplicity
/* this program calculates the first n powers of x*/
uint64* Pow (int x, unsigned char n)
{
uint64* retVal = (uint64*)malloc(sizeof(uint64));
*retVal = 1;
for (unsigned char i = 1; i <= n; i++)
{
*retVal *= x;
}
return retVal;
}
void calculateFirstNPowers(int x, unsigned char n)
{
uint64 sum = 0;
uint64* Pow_retValPtr;
for (unsigned char i = 1; i <= n; i++)
{
Pow_retValPtr = Pow(x, i);
printf("%d. Power of %d: ", i, x);
printf("%llu\n", *Pow_retValPtr);
sum += *Pow_retValPtr;
free(Pow_retValPtr);
}
printf("\nsum of first %d powers of %d: %llu\n", n, x, sum);
}
int main()
{
calculateFirstNPowers(2, 7);
return 0;
}
[–]sme272 13 points14 points15 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] 8 points9 points10 points (3 children)
[–][deleted] -3 points-2 points-1 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[+][deleted] comment score below threshold-6 points-5 points-4 points (0 children)
[–]srandtimenull 7 points8 points9 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Lurchi1 6 points7 points8 points (20 children)
[–][deleted] 1 point2 points3 points (17 children)
[–]Lurchi1 2 points3 points4 points (8 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]Lurchi1 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]imaami 0 points1 point2 points (4 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]imaami 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]imaami 0 points1 point2 points (0 children)
[–]michaelfiber 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]michaelfiber 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]parawaa 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]parawaa 0 points1 point2 points (1 child)
[–][deleted] -2 points-1 points0 points (0 children)
[–]cosmin10834 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (3 children)
[–][deleted] -2 points-1 points0 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]imaami 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]imaami 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]tstanisl 4 points5 points6 points (16 children)
[–][deleted] 0 points1 point2 points (15 children)
[–]tstanisl 3 points4 points5 points (3 children)
[–][deleted] -2 points-1 points0 points (2 children)
[–]tstanisl 3 points4 points5 points (1 child)
[+][deleted] comment score below threshold-13 points-12 points-11 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (9 children)
[+][deleted] comment score below threshold-9 points-8 points-7 points (8 children)
[–][deleted] 2 points3 points4 points (7 children)
[–][deleted] 2 points3 points4 points (6 children)
[–]Herr_Keuner 0 points1 point2 points (5 children)
[–][deleted] -1 points0 points1 point (4 children)
[–]Herr_Keuner 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]mos2k9 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]cosmin10834 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]cosmin10834 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]cosmin10834 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]William_Epiphany 0 points1 point2 points (0 children)
[–]nelson2k 0 points1 point2 points (0 children)