#Modifying string elements within a function gives a segmentation fault.

25 messages · Page 1 of 1 (latest)

placid parcel
#

So, I'm trying to do a caesar cipher for CS50, and I'm modifying the contents of an array within a function. I have a for loop that goes through the string, I take a single element of the string, and I'm setting it to something, and the moment I do, I get a segmentation fault. Could anyone explain to me what's going on?

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void shift(string sentence, int number);

int main(void)
{
    string sentence = "abcde"; // dummy array
    int number = 1; // we are shifting it by one, so a becomes b etc
    shift(sentence, number);
    printf("%s\n", sentence);
}

void shift(string sentence, int number)
{
    for (int i = 0, n = strlen(sentence); i < n; i++) // we loop through it from zero to the length of the string
    {
        if (isalpha(sentence[i]) != 0) // if it is not a letter, we skip it
        {
            sentence[i] = (int) sentence[i] + number; // I get a segmentation fault here
        }
    }
    return;
}```
tribal pastureBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

worn mango
#

;compile

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void shift(char *sentence, int number);

int main(void)
{
    char sentence[] = "abcde"; // dummy array
    int number = 1; // we are shifting it by one, so a becomes b etc
    shift(sentence, number);
    printf("%s\n", sentence);
}

void shift(char *sentence, int number)
{
    for (int i = 0, n = strlen(sentence); i < n; i++) // we loop through it from zero to the length of the string
    {
        if (isalpha(sentence[i]) != 0) // if it is not a letter, we skip it
        {
            sentence[i] = (int) sentence[i] + number; // I get a segmentation fault here
        }
    }
    return;
}
desert juniperBOT
#
Program Output
bcdef
worn mango
#

sentence needs to be an array of characters that can be mutated

placid parcel
#

uh, okay? I thought about replacing the string with a normal char array before, but I'm not sure how to get input from the user then. There's a function in the CS50 library that gets a string from the user, and I guess I could just copy over everything from it?

#

I've no idea what char* means, even the reference just says "think of this as a string", but I don't want to waste your time, under what term should I google it?

#

nvm, found something on stack overflow on it

#

ah, I get it now. why the hell is this a thing?

worn mango
placid parcel
#

yea

worn mango
#

if so, it looks like it allocates its memory each time instead of returning a string literal

placid parcel
#

uh, as I understand it, it doesn't create an array, but a pointer to an array that's read-only

#

where the actual data is

worn mango
#

from the description, it creates a new array each time with space sufficient for the whole string (probably using malloc)

#

it looks like they intended it to not be read only, considering that it returns a pointer to mutable data

#

but that API is very strange

placid parcel
#

well if that's what they intended, then they done goofed up

#

anyway, I'm gonna convert the string into a char[], I'll close this thread if it works out

worn mango
#

looking at the source code for get_string it looks like it does really create a new allocated array of characters each time

#

and it cleans them up at exit itself for some reason

placid parcel
#

alright, eventually just recoded it so that instead of first processing the entire string and then printing it at once, the function only processes a single character at once, prints it instantly, and it's in a loop so it goes through the whole string

#

thanks for taking your time

#

"solved