Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

Help in modifying a cs50 string

0 votes
asked May 30, 2018 in Pset2 by Rwitaban (140 points)
#include <cs50.h>

int main(void)
{
    string str = "a";
    str[0] = 'b';
}

 

I expected that this would modify the string str to make it "b". But this instead gives me a segfault at exactly the line str[0] = 'b'. Is this a quirk of the implementation of string in the cs50 library? If so, how can we modify a string after declaring it? Do we have to declare it explicitly as a char array?

Please log in or register to answer this question.

Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

197 questions

248 answers

217 comments

4,952 users

5 Online
0 Member And 5 Guest
...