我想制作一个包含字符串矩阵的结构
I want to make a struct that includes a matrix of string
我想制作一个包含字符串矩阵的结构。
赞 |0|1|2|..|10
每个位置都应该有这样的字符串:hello, world, 1234, ...
除非达到限制 (= SO_BLOCK_SIZE),否则我想添加字符串,因此我创建了一个函数来了解我已经添加了多少字符串。我遇到了一些这样的错误:
错误:预期的声明说明符或数字常量前的“...”
#define SO_REGISTRY_SIZE 10
注意:在宏“SO_REGISTRY_SIZE”的扩展中
char (*矩阵)(SO_REGISTRY_SIZE);
警告:结构或联合的末尾没有分号
错误:“libroMastro”{又名“struct libroMastro”}没有名为“矩阵”的成员
如果((libro->矩阵[i][j])== NULL)
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 64
#define SO_REGISTRY_SIZE 10
#define SO_BLOCK_SIZE 5
typedef struct libroMastro{
char (*matrice)(SO_REGISTRY_SIZE);
}libroMastro;
int whatIndex(libroMastro *libro){
int i = 0;
int j = 0;
for(i; i < SO_REGISTRY_SIZE; i++){
for(j; j < SO_BLOCK_SIZE; j++){
if((libro->matrice[i][j]) == NULL)
return j;
}
}
return j;
}
int main(){
libroMastro *libro;
whatIndex(libro);
}
您的代码在许多地方无效。
我会这样实现:
typedef struct libroMastro
{
size_t nmessages;
char *matrice[];
}libroMastro;
libroMastro *addString(libroMastro *lm, const char *str)
{
if(str)
{
size_t newsize = lm ? lm -> nmessages + 1 : 1;
lm = realloc(lm, sizeof(*lm) + newsize * sizeof(lm -> matrice[0]));
if(lm)
{
if((lm -> matrice[newsize - 1] = strdup(str)))
{
lm -> nmessages = newsize;
}
else
{
lm -> nmessages = newsize - 1;
}
}
}
return lm;
}
size_t how_many(libroMastro *lm)
{
if(lm) return lm -> nmessages;
}
int main(){
libroMastro *libro = NULL;
libro = addString(libro, "Hello");
libro = addString(libro, "World");
libro = addString(libro, "Next");
printf("Libro contains %zu strings\n", libro -> nmessages);
}
我想制作一个包含字符串矩阵的结构。
赞 |0|1|2|..|10 每个位置都应该有这样的字符串:hello, world, 1234, ...
除非达到限制 (= SO_BLOCK_SIZE),否则我想添加字符串,因此我创建了一个函数来了解我已经添加了多少字符串。我遇到了一些这样的错误:
错误:预期的声明说明符或数字常量前的“...” #define SO_REGISTRY_SIZE 10
注意:在宏“SO_REGISTRY_SIZE”的扩展中 char (*矩阵)(SO_REGISTRY_SIZE);
警告:结构或联合的末尾没有分号
错误:“libroMastro”{又名“struct libroMastro”}没有名为“矩阵”的成员 如果((libro->矩阵[i][j])== NULL)
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 64
#define SO_REGISTRY_SIZE 10
#define SO_BLOCK_SIZE 5
typedef struct libroMastro{
char (*matrice)(SO_REGISTRY_SIZE);
}libroMastro;
int whatIndex(libroMastro *libro){
int i = 0;
int j = 0;
for(i; i < SO_REGISTRY_SIZE; i++){
for(j; j < SO_BLOCK_SIZE; j++){
if((libro->matrice[i][j]) == NULL)
return j;
}
}
return j;
}
int main(){
libroMastro *libro;
whatIndex(libro);
}
您的代码在许多地方无效。
我会这样实现:
typedef struct libroMastro
{
size_t nmessages;
char *matrice[];
}libroMastro;
libroMastro *addString(libroMastro *lm, const char *str)
{
if(str)
{
size_t newsize = lm ? lm -> nmessages + 1 : 1;
lm = realloc(lm, sizeof(*lm) + newsize * sizeof(lm -> matrice[0]));
if(lm)
{
if((lm -> matrice[newsize - 1] = strdup(str)))
{
lm -> nmessages = newsize;
}
else
{
lm -> nmessages = newsize - 1;
}
}
}
return lm;
}
size_t how_many(libroMastro *lm)
{
if(lm) return lm -> nmessages;
}
int main(){
libroMastro *libro = NULL;
libro = addString(libro, "Hello");
libro = addString(libro, "World");
libro = addString(libro, "Next");
printf("Libro contains %zu strings\n", libro -> nmessages);
}