1

UPDATE: After posing my original question (please see below), I learned more about variable scope, memory management, etc. I went with the straightforward solution of defining a variable in main(), passing it to my replace() function, changing it in-place, then returning it to main(), so that the variable is always in scope. Please see the updated code block below.

Now I am observing strange behaviour in the VM workspace that I do not think is a consequence of my script.

First: when I run my script in the workspace IDE, it prints the correct value, but only some of the time: for every ten or so runs, I might get 3 or 4 correct prints to screen.

Second: when I use debug50 in the workspace IDE, the variable is assigned correctly, and as I step through main(), all steps are executed correctly each time, including the print to screen.

Third: when I run check50 for this problem, the stochastic print behaviour occurs. Check50 returns some correct tests and some incorrect tests.

Fourth: when I run this program locally on my home machine (WSL2) using the gcc compiler and manually linking the cs50 header file, it runs perfectly every time. No hiccups. edit: for inputs less than 15 characters long; for longer inputs, the first 15 chars are gibberish. There's another issue here. NB: using gcc

Fifth: when I compile this program in the cs50 workspace using the gcc compiler explicitly (gcc no-vowels.c -o no-vowels), the unusual behaviour changes: I get a printout each time the program is run, but longer inputs result in truncated outputs. So, a five-letter word will print fine every time, but a ten-letter word (e.g. pseudocode) will print out every time, but consistently truncated: ps3ud0c0 This truncation does not occur on my local machine when using the same compiler.

Based on these observations, I propose that there is an issue somewhere in the cs50 workspace, and/or with the compiler that make is using. Can anyone replicate this behaviour?

** BEGIN OLD QUESTION ** Struggling with the no-vowels practice problem. Up until now, we've only been asked to print from functions and return either void or integers; now, I'm struggling to return my string from replace() to main() as specified in the assignment description.

When I run the debugger, I can see that the local variable in replace() is correctly assigned with my answer. But when I step the debugger to the subsequent line to call printf() in main(), the variable sent from replace() becomes empty (deallocated? dereferenced? out of scope?).

We haven't gotten to pointers or memory management yet (week 4 I believe), and I haven't reached that topic yet in my K&R C textbook either. So while I have an idea of what pointers are, I have no clue how to use or assign or reference them. StackOverflow has told me that the variables I'm using in my replace() function are pointers to memory addresses containing my array, but I just don't know how to handle this yet. I'm used to Python, where I can throw objects around my scripts like footballs.

I tried getting around this by setting a global variable, having replace() edit the global variable, and then having main() print it; this led to a segmentation fault.

What is the correct way to return my string (char array) from replace() to main() so that I can printf it as per the question specs?

Thank you in advance! ** END OLD **

UPDATED code:

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

// function declarations
bool argcheck (int argc, string argv[]);
string replace(string input);

// #######################################
// MAIN
int main(int argc, string argv[])
{
    if (argcheck(argc, argv) == false)
    {
        return 1;
    }
    else
    {
        string input = argv[1];
        input = replace(input);
        printf("%s\n", input);
    }
    return 0;
}

// #######################################
// FUNCTIONS

// input validation
bool argcheck (int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./no-vowels word\n");
        return false;
    }
    return true;
}

// converter implementation
string replace(string input)
{
    char leeted[strlen(input)+1];

    int i = 0;
    while (input[i] != '\0')
    {
        switch(input[i])
        {
            case 'a':
                leeted[i] = (char) '6';
                break;

            case 'e':
                leeted[i] = (char) '3';
                break;

            case 'i':
                leeted[i] = (char) '1';
                break;

            case 'o':
                leeted[i] = (char) '0';
                break;

            default:
                leeted[i] = input[i];
                break;
        }
        i++;
    }
    leeted[i] = '\0';
    input = (string) leeted;
    // printf("from replace(): %s\n", input);
    return input;
}
[![inconsistent output in cs50 workspace IDE from no-vowels program][1]][1]


  [1]: https://i.sstatic.net/mO1IH.png
8
  • Are you using the IDE? This code works correctly on the IDE, passes check50 and has no valgrind errors (memory checker). (Ditto the version posted on SO) The simplest explanation I can think of is you are not running a compiled version of this code. Commented Jan 17, 2023 at 12:04
  • Hello @DinoCoderSaurus I am using VSCode in a cs50 workspace. I am most definitely compiling and running correctly. I'm editing my question, there's been a significant update; I'd really appreciate your input because I have compelling evidence that there is a problem with the workspace. I'll ping you again when I've finished my update. Thanks
    – skytwosea
    Commented Jan 17, 2023 at 12:44
  • Hello again @DinoCoderSaurus I've updated my question to reflect current behaviour. I'd really appreciate any input you can provide! Thanks
    – skytwosea
    Commented Jan 17, 2023 at 13:00
  • Most perplexing. I ran this version of the code with "abcd.....z" 15 times, successful each time. check50 (local) success. no memory errors reported by valgrind. I can't argue with your assessment of a workspace problem. My cs50 extension version is 0.0.1; haven't been able to find whether that is the current version (i have run update50 countless times). didn't find similar on the subreddit or Ed discussion. I have lots of ideas on how to streamline the code, but that will not help in finding root cause. I will be away for the next couple of hours, but will pick this up again after lunch. Commented Jan 17, 2023 at 13:53
  • @DinoCoderSaurus Thanks again. First, I'm very happy to hear code criticism; that's why I'm here after all! To learn. But yes, I'd like to get this root issue sorted out first. I'm very open to the idea that I'm still botching something, but evidence is currently pointing toward an interesting issue with the compiler and/or workspace. I'm at work all day (eastern standard time) but will be here after my day is done. Looking forward to hearing your thoughts.
    – skytwosea
    Commented Jan 17, 2023 at 14:14

3 Answers 3

0

One "problem" is that leeted in the replace function is declared as a Variable Length Array (VLA)

The key takeaway for me:

...lifetime of a VLA ends when the declaration goes out of scope

Change the declaration to string. [That corrected the output on my repro fail]

1
  • I had tried to do this in my first implementations, but when the leeted array is of string type, there are a litany of type errors that I haven't figured out how to work through. I'm at work for the next four hours but will give it another go this evening and get back to you.
    – skytwosea
    Commented Jan 17, 2023 at 19:53
0

I'm getting on topic. I have a slightly different problem - in the replace function, it assigns the letter/number values ​​to a new type named char after_word[n]; unfortunately I don't know how to convert char type to string type?enter image description here

0
0

ok finally done it (but with for) loop. I just overprint the word string with new char and it works! enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .