将字符串数组传递给要更改的函数

时间:2016-10-24 19:21:09

标签: c arrays

所以目前我正在运行一个程序来随机生成一个文件包,然后让它们相互连接,并创建一个迷宫般的游戏。 我正在尝试构建一个文件路径数组以传递给一个函数,这样它就可以生成,然后调用函数可以对数组进行更多的处理。正在发生的是生成数组但是将第一个元素(filepath [0])留空而因此seg。发生在我身上。但是当我设置断点时,数组的所有其他部分都很好,而不是第一个元素。自从我写C和C以来已经过了大约9个月而且我不确定我的指针打嗝来自哪里,谢谢大家先进

这是迄今为止的代码

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>

void create_files(char (*filepath[7]));

int main(){
    time_t t;
    char *filepath[7];
    srand((unsigned) time(&t));
    int i = 0;
    for (i = 0; i < 7; i++)
        filepath[i] = malloc(60);


    create_files(filepath);
    for (i = 0; i < 7; i++)
        free(filepath[i]);
    return 0;
}

void create_files(char (*filepath[7])){
    int i = 0, pid = getpid(),q = 0,random, r=7;
    char procid[20];
    sprintf(procid, "%d", pid);
    char directory[80] = "./dringb.rooms.";

    strcat(directory,procid);
    int newdir = mkdir(directory, 0777);

    for (q = 0; q < 7; q++)
        filepath[q] = directory;

    char *bigrooms[10] ={"/Bridge.txt","/Gate.txt","/Hallway.txt",
        "/Dungeon.txt","/Galley.txt","/Throne.txt","/Boss.txt", "/Lab.txt",
        "/Torture.txt", "/Courtyard.txt"};
    bool redflag = false;
    char *rooms[7];
    q = 0;

    while (q != 7){ //grabs the rooms at random from the set of bigrooms
        random = rand()%10;
        for(i = 0; i < 7; i++){
            if (rooms[i] == bigrooms[random])
                redflag = true;
        }
        if (redflag == false){
            rooms[q] = bigrooms[random];
            redflag = false;
            q++;
        }
        else
            redflag = false;
    }

    char **dest = (char **)malloc(r * sizeof(char *));
    for (i=0; i<r; i++)
        dest[i] = (char *)malloc(8 * sizeof(rooms)); //allocates each room a new space

    for (i = 0; i < 7; i++){
        strcat(dest[i], directory);
        strcat(dest[i],rooms[i]);
        filepath[i] = dest[i]; //creates directory path for each room.txt
    }


    int usedrooms[4];
    for (i = 0; i < 7; i++){
        FILE *f = fopen(filepath[i], "w");
        fputs("Roomname: ", f);
        fputs(rooms[i],f);
        fputs("\n",f);
        fclose(f);
    }

    for (i = 0; i < 7; i++){
        FILE *f = fopen(filepath[i], "a+");
        for (q = 0; q < 4; q++)
            usedrooms[q] = 100;
        int roomrand, q = 0, z = 0, connrooms = 3;
        bool greenflag = true, retry = false;

        roomrand = rand() %2;
        if (roomrand == 1)
            connrooms = 4;

        while (q != connrooms){ //prevents from having a connection to same room
            do{
                retry = false;
                roomrand = rand() % 7;
                for(z = 0; z < 4; z++){
                    if (roomrand == usedrooms[z])
                        retry = true;
                }
            }while(roomrand == i || retry == true); //prevents from having a connection to same room
            bool found = false;
            char buffer[100];
            rewind(f);
            while(fscanf(f,"%s", buffer) == 1){
                if (strcmp(buffer,rooms[roomrand]) == 0)//prevents a double connecting room from being added
                    greenflag = false;
            }
            if(greenflag == true){
                usedrooms[q] = roomrand;
                fputs("Connecting Room: ", f);
                fputs(rooms[roomrand],f);
                fputs("\n",f);
            }

            fclose(f);
            greenflag = true;
            found = false;
            FILE *f2 = fopen(filepath[roomrand],"a+");
            rewind(f2);
            while(fscanf(f2,"%s", buffer) == 1){
                if (strcmp(buffer,rooms[i]) == 0) //prevents a double connecting room from being added
                    found = true;
            }
            if (found == false){
                fputs("Connecting Room: ",f2);
                fputs(rooms[i],f2);
                fputs("\n",f2);
            }
            fclose(f2);
            fopen(filepath[i],"a+");
            found = false;
            q++;
        }
        q = 0;
        fclose(f);
    }

    int usedroomtype[7];
    int roomrand;
    for (i = 0; i < 7; i++)
        usedroomtype[i] = 100;

    for (i = 0; i < 7;i++){
        do{
            redflag = false;
            roomrand = rand() % 7;
            for (q = 0; q < 7; q++)
                if (roomrand == usedroomtype[q])
                    redflag = true;
        } while (redflag == true);
        usedroomtype[i] = roomrand;
        FILE *fp = fopen(filepath[roomrand], "a+");
        if (i == 0)
            fputs("Room Type: Start Room", fp);
        else if (i == 6)
            fputs("Room Type: End Room",fp);
        else
            fputs ("Room Type: Mid Room",fp);
        fclose(fp);
    }

}

1 个答案:

答案 0 :(得分:0)

正确传递数组。问题是数据已损坏。

for (q = 0; q < 7; q++)
    filepath[q] = directory;

这是无效的。它应该是strcpy(filepath[q], directory);可以设置为char *temp = filepath[q],因为temp未分配。但filepath[q]已经分配。使用strcpy更改其值。

后来出现了类似的错误

char **dest = (char **)malloc(r * sizeof(char *));
for (i=0; i<r; i++)
    dest[i] = (char *)malloc(8 * sizeof(rooms));

for (i = 0; i < 7; i++){
    strcat(dest[i], directory);
    strcat(dest[i],rooms[i]);
    filepath[i] = dest[i]; //creates directory path for each room.txt
}

两件事。首先,dest未初始化。始终使用未初始化的字符串strcpy开头,然后使用strcat。其次,如前所述,使用strcpy更改filepath[i]的值。实际上不需要dest。您可以直接复制到filepath

for (i = 0; i < 7; i++)
{
    strcpy(filepath[i], directory);
    strcat(filepath[i], rooms[i]);
}

正如评论中所述,filepath的分配应该更大。 directory最多80个字节,room最多10个字节,因此filepath最多应为90个字节。

for (i = 0; i < 7; i++)
    filepath[i] = malloc(90);

还有一些值未初始化,例如char *rooms[7];

其他地方:

int pid = getpid();
char procid[20];
sprintf(procid, "%d", pid);
char directory[80] = "./dringb.rooms.";
strcat(directory,procid);

您已使用sprintf,您可以按如下方式简化:

sprintf(directory, "./dringb.rooms.%d", getpid());

示例:

int main()
{
    time_t t;
    srand((unsigned) time(&t));

    char *filepath[7];
    int i = 0;
    for (i = 0; i < 7; i++)
        filepath[i] = malloc(90);

    create_files(filepath);

    for (i = 0; i < 7; i++)
        printf("%s\n", filepath[i]);

    for (i = 0; i < 7; i++)
        free(filepath[i]);

    return 0;
}
void create_files(char *filepath[7])
{
    int i = 0, random;
    char directory[80];
    sprintf(directory, "./dringb.rooms.%d", getpid());
    //mkdir(directory);
    mkdir(directory, 0777);

    int q;
    for (q = 0; q < 7; q++) strcpy(filepath[q], directory);

    char *bigrooms[20] ={"/Bridge.txt","/Gate.txt","/Hallway.txt","/Dungeon.txt","/Galley.txt","/Throne.txt","/Boss.txt", "/Lab.txt","/Torture.txt", "/Courtyard.txt"};
    bool redflag = false;
    char *rooms[7];
    for (i = 0; i < 7; i++) rooms[i] = 0;

    q = 0;
    while (q != 7)
    {
        //grabs the rooms at random from the set of bigrooms
        random = rand()%10;
        for(i = 0; i < 7; i++)
        {
            if (rooms[i] == bigrooms[random])
                redflag = true;
        }

        if (redflag == false)
        {
            rooms[q] = bigrooms[random];
            redflag = false;
            q++;
        }
        else
            redflag = false;
    }

    for (i = 0; i < 7; i++)
    {
        strcpy(filepath[i], directory);
        strcat(filepath[i], rooms[i]);
    }

    int usedrooms[4];
    for (i = 0; i < 7; i++)
    {
        FILE *f = fopen(filepath[i], "w");
        fputs("Roomname: ", f);
        fputs(rooms[i],f);
        fputs("\n",f);
        fclose(f);
    }

    for (i = 0; i < 7; i++)
    {
        FILE *f = fopen(filepath[i], "a+");
        for (q = 0; q < 4; q++)
            usedrooms[q] = 100;
        int roomrand, q = 0, z = 0, connrooms = 3;
        bool greenflag = true, retry = false;

        roomrand = rand() %2;
        if (roomrand == 1)
            connrooms = 4;

        while (q != connrooms)
        { //prevents from having a connection to same room
            do
            {
                retry = false;
                roomrand = rand() % 7;
                for(z = 0; z < 4; z++)
                {
                    if (roomrand == usedrooms[z])
                        retry = true;
                }
            }
            while(roomrand == i || retry == true); //prevents from having a connection to same room

            bool found = false;
            char buffer[100];
            rewind(f);
            while(fscanf(f,"%s", buffer) == 1)
            {
                if (strcmp(buffer,rooms[roomrand]) == 0)//prevents a double connecting room from being added
                    greenflag = false;
            }

            if(greenflag == true)
            {
                usedrooms[q] = roomrand;
                fputs("Connecting Room: ", f);
                fputs(rooms[roomrand],f);
                fputs("\n",f);
            }

            fclose(f);
            greenflag = true;
            found = false;
            FILE *f2 = fopen(filepath[roomrand],"a+");
            rewind(f2);

            while(fscanf(f2,"%s", buffer) == 1)
            {
                if (strcmp(buffer,rooms[i]) == 0) //prevents a double connecting room from being added
                    found = true;
            }

            if (found == false)
            {
                fputs("Connecting Room: ",f2);
                fputs(rooms[i],f2);
                fputs("\n",f2);
            }

            fclose(f2);
            fopen(filepath[i],"a+");
            found = false;
            q++;
        }
        q = 0;
        fclose(f);
    }

    int usedroomtype[7];
    int roomrand;
    for (i = 0; i < 7; i++)
        usedroomtype[i] = 100;

    for (i = 0; i < 7; i++)
    {
        do
        {
            redflag = false;
            roomrand = rand() % 7;
            for (q = 0; q < 7; q++)
                if (roomrand == usedroomtype[q])
                    redflag = true;
        }
        while (redflag == true);

        usedroomtype[i] = roomrand;
        FILE *fp = fopen(filepath[roomrand], "a+");
        if (i == 0)
            fputs("Room Type: Start Room", fp);
        else if (i == 6)
            fputs("Room Type: End Room",fp);
        else
            fputs ("Room Type: Mid Room",fp);
        fclose(fp);
    }
}