Подтвердить что ты не робот

Пропустить структуру по ссылке в C

Правильно ли этот код? Он работает так, как ожидалось, но правильно ли этот код использует указатели и точечную нотацию для структуры?

struct someStruct {
 unsigned int total;
};

int test(struct someStruct* state) {
 state->total = 4;
}

int main () {
 struct someStruct s;
 s.total = 5;
 test(&s);
 printf("\ns.total = %d\n", s.total);
}
4b9b3361

Ответ 1

Ваше использование указателей и точечных обозначений является хорошим. Компилятор должен сообщить вам об ошибках и/или предупреждениях, если возникла проблема.

Вот копия вашего кода с некоторыми дополнительными примечаниями и вещами, чтобы думать о том, как использовать структуры и указатели, функции и область переменных.

// Define the new variable type which is a struct.
// This definition must be visible to any function that is accessing the
// members of a variable of this type.
struct someStruct {
    unsigned int total;
};

/*
 * Modifies the struct that exists in the calling function.
 *   Function test() takes a pointer to a struct someStruct variable
 *   so that any modifications to the variable made in the function test()
 *   will be to the variable pointed to.
 *   A pointer contains the address of a variable and is not the variable iteself.
 *   This allows the function test() to modify the variable provided by the
 *   caller of test() rather than a local copy.
 */
int test(struct someStruct *state) {
    state->total = 4;
    return 0;
}

/* 
 * Modifies the local copy of the struct, the original
 * in the calling function is not modified.
 * The C compiler will make a copy of the variable provided by the
 * caller of function test2() and so any changes that test2() makes
 * to the argument will be discarded since test2() is working with a
 * copy of the caller variable and not the actual variable.
 */
int test2(struct someStruct state) {
    state.total = 8;
    return 0;
}

int test3(struct someStruct *state) {
    struct someStruct  stateCopy;
    stateCopy = *state;    // make a local copy of the struct
    stateCopy.total = 12;  // modify the local copy of the struct
    *state = stateCopy;    /* assign the local copy back to the original in the
                              calling function. Assigning by dereferencing pointer. */
    return 0;
}

int main () {
    struct someStruct s;

    /* Set the value then call a function that will change the value. */
    s.total = 5;
    test(&s);
    printf("after test(): s.total = %d\n", s.total);

    /*
     * Set the value then call a function that will change its local copy 
     * but not this one.
     */
    s.total = 5;
    test2(s);
    printf("after test2(): s.total = %d\n", s.total);

    /* 
     * Call a function that will make a copy, change the copy,
       then put the copy into this one.
     */
    test3(&s);
    printf("after test3(): s.total = %d\n", s.total);

    return 0;
}

Ответ 2

Правильное использование структуры. Есть вопросы о ваших возвращаемых значениях.

Кроме того, поскольку вы печатаете неподписанный int, вы должны использовать %u вместо %d.

Ответ 3

Да, это правильно. Он создает struct s, устанавливает его значение в 5, передает указатель на него функции, которая использует указатель, чтобы установить общее число в 4, а затем распечатывает его. -> для членов указателей на структуры и . для членов структур. Так же, как вы использовали их.

Возвращаемые значения разные. test, вероятно, будет недействительным, а main нуждается в return 0 в конце.

Ответ 4

Угу. Это правильно. Если это не было (с точки зрения. /- > ), ваш компилятор будет кричать.

Ответ 5

Да, его правильное использование структур. Вы также можете использовать

typedef struct someStruct {
 unsigned int total;
} someStruct;

Тогда вам не придется писать struct someStruct s; снова и снова, но затем можно использовать someStruct s;.