可以通过c中的字符访问结构成员吗?
Able to access struct member through character in c?
基本上我有一个包含多个成员的结构,名为 a-z。我有一个字符串,我希望每个字母都对应于正确的结构成员。目前我对每个案例都使用 switch 语句以访问结构的正确字母成员,但我想知道是否有更好的(cleaner/shorter 代码)方法来执行此操作,而不必将 26 个案例语句与近内部代码相同?我的代码看起来有点像这样:
typedef struct node
{
struct node *a;
struct node *b;
struct node *c;
...
struct node *z;
}node;
node *nTable[26][26][27];
int main
{
...
node *nWord = malloc(sizeof(node));
node *nPath = nWord;
nTable[0][0][0] = nWord;
char *cWord
cWord = "abcde";
for (int n = 0; n < 5; n++)
{
nWord = malloc(sizeof(node));
switch (cWord[n])
case 'a':
nPath->a = nWord;
nPath = nWord;
break;
case 'b':
nPath->b = nWord;
nPath = nWord;
...
case 'z':
nPath->z = nWord; //code is the same for each case, only difference between each is which member its assigned to
nPath = nWord;
}
}
请注意,以上代码是我实际代码的简化形式,因此此示例中可能存在一些基本语法和其他错误,但我的代码的一般用途应该是显而易见的。
那么有没有更简洁的方法来做到这一点,而不是对每个字母都使用 case 语句?一些简单的东西,比如“nPath->cWord[n] = code;”将会是完美的! (虽然这样,显然行不通)
任何想法都会很棒!如果我遗漏了任何重要信息,请提前致歉
对于初学者这个结构
char *cWord = {a,b,c,d,e};
语法无效。您不能使用带有多个初始化程序的花括号列表来初始化标量对象。
至于你问什么时候可以在结构中声明一个数组类型的数据成员而不是像
这样的许多数据成员
struct node *a;
例如
#define N 26
typedef struct node
{
struct node *nodes[N];
} node;
然后使用下面的方法
const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *p = strchr( letters, toupper( ( unsigned char )cWord[i] ) );
if ( p != NULL )
{
nPath->nodes[p - letters] = data;
// or
// nPath->nodes[p - letters] = malloc( sizeof( node ) );
}
也许你可以把注意力集中在单词上:首先你有一个单词链表。然后每个字母都会有一个链表,以便构建一个单词链表数组。
考虑这个例子:
#include <stdio.h>
typedef struct _node
{
char* word;
struct _node* next;
struct _node* prev;
}; // single node contains one word
typedef struct _node Node;
typedef struct
{
char letter;
unsigned size;
unsigned limit;
Node* start;
Node* end;
} _list;
typedef _list Data;
这样你就有了更多的结构
查看此代码
#include "x25list.h"
int main(int argc, char** argv)
{
Data* db[26];
for (int i = 'a'; i < 'z'; i += 1)
{ // builds initial linked lists
int ix = i - 'a';
db[ix] = (Data*)malloc(sizeof(Data));
db[ix]->letter = i;
db[ix]->size = 0;
db[ix]->end = NULL;
db[ix]->start = NULL;
}; // for()
return 0;
};
In this way you have a linked list for each letter already built and start to catalog words using the notes on the lists pointed by the value of the letter minus the value of 'a'.
基本上我有一个包含多个成员的结构,名为 a-z。我有一个字符串,我希望每个字母都对应于正确的结构成员。目前我对每个案例都使用 switch 语句以访问结构的正确字母成员,但我想知道是否有更好的(cleaner/shorter 代码)方法来执行此操作,而不必将 26 个案例语句与近内部代码相同?我的代码看起来有点像这样:
typedef struct node
{
struct node *a;
struct node *b;
struct node *c;
...
struct node *z;
}node;
node *nTable[26][26][27];
int main
{
...
node *nWord = malloc(sizeof(node));
node *nPath = nWord;
nTable[0][0][0] = nWord;
char *cWord
cWord = "abcde";
for (int n = 0; n < 5; n++)
{
nWord = malloc(sizeof(node));
switch (cWord[n])
case 'a':
nPath->a = nWord;
nPath = nWord;
break;
case 'b':
nPath->b = nWord;
nPath = nWord;
...
case 'z':
nPath->z = nWord; //code is the same for each case, only difference between each is which member its assigned to
nPath = nWord;
}
}
请注意,以上代码是我实际代码的简化形式,因此此示例中可能存在一些基本语法和其他错误,但我的代码的一般用途应该是显而易见的。
那么有没有更简洁的方法来做到这一点,而不是对每个字母都使用 case 语句?一些简单的东西,比如“nPath->cWord[n] = code;”将会是完美的! (虽然这样,显然行不通)
任何想法都会很棒!如果我遗漏了任何重要信息,请提前致歉
对于初学者这个结构
char *cWord = {a,b,c,d,e};
语法无效。您不能使用带有多个初始化程序的花括号列表来初始化标量对象。
至于你问什么时候可以在结构中声明一个数组类型的数据成员而不是像
这样的许多数据成员struct node *a;
例如
#define N 26
typedef struct node
{
struct node *nodes[N];
} node;
然后使用下面的方法
const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *p = strchr( letters, toupper( ( unsigned char )cWord[i] ) );
if ( p != NULL )
{
nPath->nodes[p - letters] = data;
// or
// nPath->nodes[p - letters] = malloc( sizeof( node ) );
}
也许你可以把注意力集中在单词上:首先你有一个单词链表。然后每个字母都会有一个链表,以便构建一个单词链表数组。 考虑这个例子:
#include <stdio.h>
typedef struct _node
{
char* word;
struct _node* next;
struct _node* prev;
}; // single node contains one word
typedef struct _node Node;
typedef struct
{
char letter;
unsigned size;
unsigned limit;
Node* start;
Node* end;
} _list;
typedef _list Data;
这样你就有了更多的结构
查看此代码
#include "x25list.h"
int main(int argc, char** argv)
{
Data* db[26];
for (int i = 'a'; i < 'z'; i += 1)
{ // builds initial linked lists
int ix = i - 'a';
db[ix] = (Data*)malloc(sizeof(Data));
db[ix]->letter = i;
db[ix]->size = 0;
db[ix]->end = NULL;
db[ix]->start = NULL;
}; // for()
return 0;
};
In this way you have a linked list for each letter already built and start to catalog words using the notes on the lists pointed by the value of the letter minus the value of 'a'.