Hot answers tagged

3

String literals (e.g., 000.jpg) are constant expressions. They are read-only. When assigning a string literal to a variable of type char *, you get a memory address stored in that char * variable that points to a location on a data segment. Any attempt to change the contents of the string in such case results in a segmentation fault. A quick fix to that may ...


3

You're doing something else wrong. While you want to put a string into title, you're actually trying to put the return value from sprintf() into title and to put the contents of counter into a constant string surrounded by double quotes. The first parameter should be the string variable that you're trying to put the string in. The second (your first) ...


2

See the man page for sprintf here: https://reference.cs50.net/stdio.h/sprintf The function takes two arguments. This particular call to sprintf requires a third because the format has a variable in the form of %03d. You're missing the first argument which is to represent the char* to which sprintf writes. The example on CS50 Reference should help.


2

char* outfile = "000.jpg"; Actually your sprintf function looks good. I think this line is where the problem is. You've assigned outfile the address of a string literal, which will be automatically stored in a read-only portion of memory. Then your sprintf function tries to change it, your pc freaks out (because you're not allowed to touch it). So you'll ...


1

Your file name is 7 characters long, so plus the null terminator, your filename variable should point to a memory block of at least 8 characters, not 3. What happens is that sprintf prints to memory that does not belong to filename any more (as it's beyond the 3-character range for string including null-terminator), so probably tracker in this case.


1

The problem lies in your declaration of imagenr. char imagenr = 0; This creates a variable that holds a single character, not a string. Later, with the sprintf() call, the code copies an 8 char string (7 chars + end of string marker) into that variable, which overwrites adjacent memory. That adjacent memory looks to be the variable n. You need to declare ...


1

When you declare the char array, as char buffer[8], you are creating an array that holds 8 characters. (This should generally be large enough to include the \0 end of string character, but that's another discussion.) You can't change the size of the array once created. Once declared, you can access any element in that array, using the index of the array. ...


1

I solved this myself. The problem, of course, had to do with incorrect pointer use. I had created a fcn to parse out the query string. I parsed the query string into a local variable, and then set a main pointer variable to the local variable in the function--pretty stupid, huh? When program execution returned to main, the "old" variable had not yet been ...


Only top voted, non community-wiki answers of a minimum length are eligible