-2

因此,在编码时帮助我的是将我学到的所有东西整合到一个随机项目中。在编码时更好地帮助我和理解。我不久前了解了 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 回答