It's a C language string question

Asked 2 years ago, Updated 2 years ago, 39 views

This is the first chord I made. First, let me show you the code

/*

                                <1>

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_COUNT 6
#define MAX_TEMP_COUNT 100

void PRINT_ARRAY(char* pstr[], int length);
void CREATE_ARRAY(char* pstr[], int size);
void BUBBLE_SORT(char* pstr[], int length);
void SWAP(char** x, char** y);

int main() {

    char* str = NULL;

    printf("Please input string.\n");

    CREATE_ARRAY(&str, MAX_STR_COUNT);

    printf("\t[ Before Bubble Sort ]\n");
    PRINT_ARRAY(&str, MAX_STR_COUNT);

    BUBBLE_SORT(&str, MAX_STR_COUNT);

    printf("\t[ After Bubble Sort ]\n");
    PRINT_ARRAY(&str, MAX_STR_COUNT);

    return 0;
}
void PRINT_ARRAY(char* pstr[], int length) {

    for (int i = 0; i < length; i++) {

        printf("%s\n", *(pstr + i));
    }
    printf("\n");

}

void CREATE_ARRAY(char* pstr[], int size) {

    char temp[MAX_TEMP_COUNT];
    int len = 0;

    pstr = (char**)malloc(sizeof(char*) * MAX_STR_COUNT);

    if (pstr != NULL) {
        for (int i = 0; i < size; i++) {

            gets_s(temp, MAX_TEMP_COUNT);
            len = strlen(temp);

            *pstr = (char*)malloc(sizeof(char) * (len + 1));
            strcpy_s(*(pstr + i), len + 1, temp);
        }
    }
}

void BUBBLE_SORT(char* pstr[], int length) {

    int index = 0;

    for (int i = 0; i < length - 1; i++) {

        index = i;
        for (int j = i + 1; j < length; j++) {

            if (strcmp(pstr[index], pstr[j]) > 0)
                index = j;
        }

        SWAP(&pstr[index], &pstr[i]);
    }
}

void SWAP(char** x, char** y) {

    char* temp;
    temp = *x;
    *x = *y;
    *y = temp;

}

In the Create_Array() function,

In this way, char **pstr is being received to point to char *str. Now, the current figure is as shown in the picture above.

And the appearance after dynamic allocation is

When a dynamic assignment is received as above, malloc will no longer point to str because pstr will point to the str and, of course, str will not point to the dynamically assigned array.

So I modified the code above

#include <stdio.h>

int main() {

    char* str;

    str = CREATE_ARRAY(&str);
}

char* CREATE_ARRAY(char* pstr) {

    /*
    Work
    */
    return *pstr;
}

Create_array A code that does the same thing as the above code in the function and returns an address value to access the array dynamically assigned in the str of the main function. The Create_array function returned *pstr because it intended to match the dimension of str in the main function, and as a result of actual execution, str received a dynamic assignment, so it was not able to copy all of the strings and start using the first dynamic address. So the code below is the final revision.

/*

                                <2>

*/

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

#define MAX_STR_COUNT 5
#define MAX_TEMP_LENGTH 100


char* CREATE_ARRAY(char** pstr, int length);
void PRINT_ARRAY(char** pstr, int length);
void BUBBLE_SORT(char** pstr, int length);
void SWAP(char** x, char** y);

int main() {

    char* str = NULL;
    char** pstr = NULL;

    pstr = CREATE_ARRAY(&str, MAX_STR_COUNT);

    printf("\t[ Before Bubble Sort ]\n");
    PRINT_ARRAY(pstr, MAX_STR_COUNT);

    BUBBLE_SORT(pstr, MAX_STR_COUNT);

    printf("\t[ After Bubble Sort ]\n");
    PRINT_ARRAY(pstr, MAX_STR_COUNT);

    for (int i = 0; i < MAX_STR_COUNT; i++) {

        free(*(pstr + i));
    }
    free(pstr);

    return 0;

}
char* CREATE_ARRAY(char** pstr, int length) {

    char temp[MAX_TEMP_LENGTH];
    int len = 0;

    pstr = (char**)malloc(sizeof(char*) * length);
    if (pstr != NULL) {

        printf("Please input string.\n");

        for (int i = 0; i < length; i++) {

            gets_s(temp, MAX_TEMP_LENGTH);
            len = strlen(temp);

            *(pstr + i) = (char*)malloc(sizeof(char) * (len + 1));
            if (*(pstr + i) != NULL) {
                strcpy_s(*(pstr + i), len + 1, temp);
            }
            else exit(0);
        }
    }
    else
        exit(0);

    return pstr;
}

void PRINT_ARRAY(char** pstr, int length) {

    for (int i = 0; i < length; i++) {

        printf("%s\n", *pstr);
        pstr++;
    }
    printf("\n");
}

void BUBBLE_SORT(char** pstr, int length) {

    int index = 0;

    for (int i = 0; i < length - 1; i++) {

        index = i;
        for (int j = i + 1; j < length; j++) {

            if (strcmp(*(pstr + index), *(pstr + j)) > 0)

                index = j;
        }

        SWAP(&pstr[index], &pstr[i]);
    }
}

void SWAP(char** x, char** y) {

    char* temp;
    temp = *x;
    *x = *y;
    *y = temp;

}

Replace by returning the pstr two-dimensional pointer from the Create_array function and creating a char **pstr to copy from the main function. The pstr created by the main function will then be able to use up all of the dynamically allocated memory. However, there is an error in the code above, and the error content is

1.'=': 'char **' differs in levels of indirection from 'char *'
2.'return': 'char *' differs in levels of indirection from 'char **'

This is.

There are errors in the 13th and 42nd lines in the final modified code The Create_array function returned a two-dimensional pointer, and the main function also received a char **pstr I don't know why two dimensions are different.(Final question)

It's embarrassing, but the reason why I'm explaining the process of coding while drawing is that I'm thinking about moving on to c++, but I still feel difficult about the pointer and dynamic allocation. I tried to explain what I know as much as I can with pictures, and I explained the process and contents of the code in detail. If there is something I am using incorrectly, strange, or missing in the above code, I would appreciate it if you could let me know in response.

c pointer

2022-09-21 13:50

1 Answers

The return type of CREATE_ARRAY must be char **.

pstr = CREATE_ARRAY(..

I'm going to assign it, because the pstr is char **.


2022-09-21 13:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.