1

This is something similar to what I am trying to do (I skipped code which checks if memory was allocated):

    sscanf(line, "%[^\"]\"%[^\"]", tempString, tempString);
    int length = strlen("stackoverflow.com") + strlen(tempString);
    tempQuestion.link = (char *)malloc((length + 1) * sizeof(char));
    tempQuestion.link = "stackoverflow.com";
    strcat(tempQuestion.link, tempString);

Program crashes after it reaches strcat. I can't figure out what could possibly be wrong.

2 Answers 2

3

When you assign tempQuestion.link = "stackoverflow.com" you change the pointer tempQuestion.link. You want to use strncpy to copy the string.

Change the last two lines to

strncpy(tempQuestion.link, "stackoverflow.com", length);
strcat(tempQuestion.link, tempString);
Sign up to request clarification or add additional context in comments.

Comments

1

The following line causes the error,

tempQuestion.link = "stackoverflow.com";

Instead copy as follows,

strcpy(tempQuestion.link, "stackoverflow.com");

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.