1

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...

6
  • 1
    const char* array[x][y] = { {" ", "10", "blah", ...}, { "more", "strings", "here" } };. If you need them to be writable then 2 for loops and assign using strdup or similar. Commented Nov 25, 2022 at 14:01
  • As far as I understand the question, there's no special trick involved in doing what you ask. As such, I'm a bit at a loss as to how to answer. If you have attempted to write the needed code, then presenting your attempt might help us understand the nature of your uncertainty. If you have not made such an attempt, then why not? Commented Nov 25, 2022 at 14:26
  • @Lundin it is array of pointers not array of strings. Commented Nov 25, 2022 at 14:30
  • @JohnBollinger There is what i've done. Thank you for your help Commented Nov 25, 2022 at 14:34
  • @0___________ "How to store strings in an array 2d in c" It's not a 3D array of characters either, apparently. Commented Nov 25, 2022 at 14:39

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.