-2

コーディングによって、私が学んだすべてのことを 1 つのランダムなプロジェクトにまとめることは、私を助けてくれます。コーディング時に私を助け、理解を深めるため。しばらく前に getenv について学び、テストしていました。cの学習に戻り、プロジェクトを再度開くまではうまくいきました...

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

struct envvariabl3s
{
    char *userprofile;
    char *systemroot;

};

void loginscreen(void)
{
    int testbit = 4000000000;
    struct envvariabl3s *envariable;
    char *memtes;
    printf("\nWelcome to the login screen...\n");
    memtes = malloc(20 * sizeof(char));
    if(memtes == 0)
    {
        printf("Error: Not enough memory!");
    }
    strcpy(memtes, "Herp De Derp");

    printf("\nDynamically allocated memory is %s", memtes);
    free(memtes);

    envariable->userprofile = getenv("USERPROFILE"); //SEGFAULT HERE
    envariable->systemroot = getenv("SystemRoot");

    printf("\nUserProfile is: %s", envariable->userprofile);
    printf("\nSystem Root is: %s", envariable->systemroot);
    printf("%d", sizeof(testbit));
}
4

1 に答える 1

1

Envvariable は構造体へのポインターですが、それが指す構造体を作成していません。ランダムなメモリを指すだけで、そこにない構造体に割り当てるとクラッシュが発生します。おそらく malloc() を使用して割り当てられた、ポインターが指す実際の構造体が必要です。

于 2014-02-26T01:30:53.353 に答える