简体   繁体   中英

Why does my number variable change after running the 'insert' function?

I have been looking at this code of mine for my schoolwork but I cannot understand why my integer variable has changed after I passed it into a function by value although I did not alter it in the function.

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

void Q1();
void Q1_Remove (char **ptr_string);
void Q1_Insert (char **ptr_string, int length);

int main()
{
    Q1();

    return 0;
}

void Q1 () {
    int number;
    printf("How many characters do you want to input: ");
    scanf("%d", &number);
    char *string = malloc(number + 1);
    printf("Input the string: ");
    scanf("%s", string);
    printf("The string is: %s\n", string);

    int option;
    do {
        printf("Do you want to 1-Insert, 2-Remove or 3-Quit: ");
        scanf("%d", &option);
        switch (option) {
            case 1:
                Q1_Insert(&string, number);
                break;
            case 2:
                Q1_Remove(&string);
                break;
        }
        if (option == 1 || option == 2) {
            printf("Resulting string: %s\n", string);
        }
    } while (option == 1 || option == 2);

    free(string);
}

void Q1_Remove (char **ptr_string) {
    (*ptr_string)++;
}

void Q1_Insert (char **ptr_string, int length) {
    int curr_len = strlen(*ptr_string);
    int i;
    printf("length is %d and curr_len is %d", length, curr_len);
    if (curr_len == length) {
        for (i = curr_len - 1; i > 0; --i) {
            *(ptr_string + i) = *(ptr_string + i - 1);
        }
    } else {
        (*ptr_string)--;
    }
    char to_insert;
    printf("What is the character you want to insert: ");
    scanf(" %c", &to_insert);
    **ptr_string = to_insert;
}

My terminal input and output:

How many characters do you want to input: 5
Input the string: abcde
The string is: abcde
Do you want to 1-Insert, 2-Remove or 3-Quit: 1
length is 5 and curr_len is 5
What is the character you want to insert: a
Resulting string: abcde
Do you want to 1-Insert, 2-Remove or 3-Quit: 1
length is 11212224 and curr_len is 5                     // length is weird here
What is the character you want to insert:

(EDIT 2) Removed constant for variable number and added in the declaration on the top. Edited to add in the headers!

Primarily the issue is how you are shuffling characters in the string. You are doing this:

for (i = curr_len - 1; i > 0; --i) {
    *(ptr_string + i) = *(ptr_string + i - 1);
}

However, the actual string pointer is *ptr_string . Since you are using ptr_string + i , it's treating ptr_string as an array of pointers, and you are writing all over the stack memory following your string pointer defined in main . This will be what is trashing your number value, among other things.

Instead, you need to do this:

*(*ptr_string + i) = *(*ptr_string + i - 1);

Personally, I'd use array notation instead, which is far more readable:

(*ptr_string)[i] = (*ptr_string)[i - 1];

Or simply scrap the loop entirely and use memmove .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM