I have a problem with the following code:
for(i = 0;(i - 1)< n;i++) { char* b; sprintf(b, '%d', i); }
It compiles fine but when I run it it give me the infamous ‘0XC0000005 Access Violation’ error. I have tried setting b to NULL, ”, ‘0’, 0 and a bunch of other stuff but then I get the ‘0XC0000005 Access Violation’ error or ‘Expression: string != NULL. Any help would be appreciated!
sprintfwrites data into an existing buffer, which you pass into it as the first parameter. Currently you’re not specifying a value for b at all, which means (IIRC in C) the value could be anything. If you set it to NULL or 0,sprintfis going to try to write into memory starting at address 0.You need to create a buffer of the appropriate size, so that sprintf can write into it. For example:
Whether that’s actually how you want to allocate the buffer depends on what your real code wants to do with the results, of course.