我想在char *
数组中存储字符串,但我不知道如何做到这一点。2d数组中的每个单元格可以包含2位数的字母或数字。
|_|S|_|
|_|10|_|
|_|W|_|
|_|_|_|
|_|_|_|
我试过了,我使用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]+"