0

So I want to print an integer array to my document. To do it so I tried these:

Changing my integer array to char array:

char* intAtocharA(int* k,int n){
    char *z = calloc(n, sizeof(char));
    for (int i = 0; i < n; ++i)
    {
        z[i]=(char)k[i];
    }
    return z;
}

does not give me an error but won't do what I want.

For additional info in my int main I did these:

FILE* fout = fopen(argv[1],"w");  
char* l =intAtocharA(arr,arrsize);   
fprintf(fout,l);

I don't know if it is relevant but my integer array is full of 1's and 0's

0

1 Answer 1

1

l is an array of (small) integers, not a null-terminated string which is what the printf format string is supposed to be.

You need to use a loop and print all numbers one by one:

for (size_t i = 0; i < arraysize; ++i)
{
    fprintf(fout, "%hhd ", l[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.