CSES - Datatähti 2022 alku - Results
Submission details
Task:Merkkijono
Sender:rixu
Submission time:2021-10-08 00:51:44 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:16:26: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
     char* string = malloc(length + 1);
                    ~~~~~~^~~~~~~~~~~~

Code

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

#pragma GCC diagnostic ignored "-Wunused-result"

unsigned long function(unsigned long x) { return x*x-x+1; }

int main()
{
    unsigned long n;

    scanf("%lu", &n);

    unsigned long length = function(n);
    char* string = malloc(length + 1);
    string[0] = 'a';
    string[length] = 0;

    unsigned long index = 1;

    for(unsigned long i = 1; i < n; i++)
    {
        string[index] = 'a';
        for(unsigned long j = 0; j < index; j++) { string[j]++; }
        memcpy(string + index + 1, string, index);

        index = function(i + 1) + 1;
    }

    printf("%s", string);

}

#pragma GCC diagnostic pop