2

My struct looks like this:

struct Node
{
    int value;
    struct Node *A;
    struct Node *B;
    struct Node *C;
    struct Node *D;
    struct Node *E;
    struct Node *F;
    struct Node *G;
    struct Node *H;
    struct Node *I;
};

typedef struct Node *List;

And i'm trying to access one of "subnodes" via list->znak, where 'znak' is variable. However i'm getting error:

error: ‘struct Node’ has no member named ‘znak’

I don't know how to "tell" C that char is a variable.

I've written char, because "znak" mean char in my language.

2
  • 1
    char is a reserved word, C will be angry if you use it. Commented Jan 13, 2014 at 14:20
  • znak isn't reserved though and he can use that Commented Jan 13, 2014 at 14:23

5 Answers 5

7

In C, struct field names do not exist during runtime so you cannot convert a runtime character or string into a struct offset like you can in some scripting languages. In your case, I would use an array of Node * instead of having 10 separate fields.

struct Node
{
    int value;
    struct Node *children[9];
}

And to access the array you convert the characters to an array index by comparing them to the first character in your sequence.

struct Node* list = /*...*/;
int znak = 'D';
list->children[znak - 'A'] = /*...*/

Of course, once you start using an array instead of named fields, perhaps its going to be simpler to have znak be an integer index instead of a character in between A and I.

Sign up to request clarification or add additional context in comments.

8 Comments

This is the only reply so far that actually offers a solution to the problem and is worthy of a dedicated answer.
Thanks for answer. But when i'll do it via array would I be able to do something like this: int test = list->children[2]->children[5]->value? Sorry if it's a nooby question, i'm new to C.
@user2511670: Yes, that should work just fine. list->children[2] is also a Node * so you can use -> on it.
Yes you would be able to do that as long as you don't hit a "NULL" or other invalid pointer along the way.
The C standard does not guarantee that the characters “A” through “I” are consecutive. They are in common C implementations, including ASCII and EBCDIC, but code using this would fail if extended behind “I” and compiled for an EBCDIC implementation. At a minimum, the requirement that the characters be consecutive should be specified in the documentation for the source code.
|
0

Char is a reserved word in the C language - you cannot define a variable named 'char' link

1 Comment

Char isn't reserved only char is
0

There's no member named znak in the struct, hence the compiler won't allow you to use it as a member.

Comments

0

As others have stated, char is a reserved word in C. But additionally, the compiled code may not encode the letter of the node, since such names in a program are merely for human understanding. What you want to do is to use a hashtable or enums to assign the nodes, i.e., have an array of nodes.

Comments

0

Your struct Node has 10 members, called value, A, B, C, D, E, F, G, H and I.

There is no member called znak.

What you are wanting is for "znak" to mean &Node::A and then later to mean &Node::B which you can actually do in C++ but not in C, after which you access it through operator ->* or .*

Of course just because it can be done doesn't mean it is the right thing to do and even if your code were C++ here I would suggest simply having an array (or in C++ a vector) of your node pointers after which you can simply pass in an index to the one you want to access presumably in each of your items.

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.