简体   繁体   中英

How to store strings in a 2D array in C?

I want to store strings in a char * array but I don't know how I can do that. Each cell in the 2d array can contain letters or numbers with 2 digits.

|_|S|_|
|_|10|_|
|_|W|_|
|_|_|_|
|_|_|_|

I tried this, I used struct:

struct Etage {
    char Idsalles[20];
    int** etage;
    int width;
    int height;
}; 


#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "etage.h"

#define COLS 15
#define ROWS 9

Etage *createMap()
{
    Etage *e = malloc(sizeof(Etage));
    e->width = COLS;  // columns
    e->height = ROWS; // rows
    e->etage = malloc(sizeof(char *) * e->height);

    for (int i = 0; i < e->height; i += 1)
    {
        e->etage[i] = malloc(sizeof(char) * e->width);
        for (int j = 0; j < e->width; j += 1)
        {
            e->etage[i][j] = "0";
        }
    }

    return e;
}

void randomId(Etage *e, int maxSalles)
{
    srand(time(NULL));
    int i = 0;

    if (maxSalles < 10)
    {
        printf("Seulement %d disponibles, veuiller générer plus de salles.\n", maxSalles);
        return;
    }

    while (i < 10)
    {
        int id = (rand() % maxSalles) + 1; 
        int existe = testId(e, id);
        if (existe != 1)
        {
            e->Idsalles[i] = id;
            i += 1;
        }
    }
    
    return;
}

int testId(Etage *e, int id)
{
    for (int i = 0; i < 10; i += 1)
    {
        if (id == e->Idsalles[i])
        {
            return 1;
        }
    }
    return 0;
}

void printEtage(Etage *e)
{
    for (int i = 0; i < e->height; i += 1)
    {
        for (int j = 0; j < e->width; j += 1)
        {
            if(e->etage[i][j] == 0){
                printf("  ");
            }else{
                printf("%s", e->etage[i][j]);
                printf(" ");
            }
        }
        printf("\n");
    }

    printf("Id des salles de cette étage: ");
    for(int i =0; i <10;i+=1){
        printf("%d ",e->Idsalles[i]);
    }
    printf("\n");
}

void freeEtage(Etage *e)
{
    //free(e->etage);
    free(e);
}

void placerSalles(Etage* e){
    int a=ROWS/2;
    int b=COLS/2;
    //0 = Haut; 1 = Droite; 2 = Bas; 3 = Gauche
    e->etage[a][b] = e->Idsalles[0]+"\0";    // On place la premiere salle au centre de l'étage sa sera le spawner 'S'
    srand(time(NULL));
    int i = 1;
    while(i<10){
        int dir  = randomDirections();

        if(dir==0){ // On se déplace en haut
            a = a-1;    //On se déplace
            if(e->etage[a][b] == 0 && a > 0){
                e->etage[a][b] = e->Idsalles[i]+"\0";
                i+=1;
            }else{
              a = a+1;  // Sionon on revien a la derniere case connu
            }
        }else if(dir==1){   // On se déplace à droite
            b=b+1;
            if(e->etage[a][b] == 0 && b< e->width){
                e->etage[a][b] = e->Idsalles[i]+"\0";
                i+=1;
            }else{
                b = b-1;
            }
        }else if(dir==2){ // On se déplace en bas
            a = a+1;
            if(e->etage[a][b] == 0 && a>e->height){
                e->etage[a][b] = e->Idsalles[i]+"\0";
                i+=1;
            }else{
                a = a-1;
            }
        }else if(dir==3){ // On se déplace à gauche
            b = b-1;
            if(e->etage[a][b] == 0 && b>0){
                e->etage[a][b] = e->Idsalles[i]+"\0";
                i+=1;
            }else{
                b = b+1;
            }
        }
    }
}

int randomDirections(){
    int i = 0;
    int id = rand() % 4; // Remplacé 10 par le nbrSalles
    //printf("Position: %d\n",id);
    return id;
}

I have a good understanding with 2d array but three...

I tried using malloc .

I don't even think this is possible...

If your string is a maximum of 2 characters long (your strings will be 3 chars long)

char array[rows][cols][3] = {{"2", "a", "34"}, ... };

or to use malloc

char (*array)[cols][3] = malloc(rows * sizeof(*array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM