0

I've defined nodes in a linked list with the letters A through H like so:

A = (struct node*) malloc(sizeof *A);

I scanf in a string which names the next node and the name of the variable to change it to. So if I write "0B", I want it to take the character 'B' and point to the object that has the variable name B:

state->zero = B;

without having to explicitly write a function where I have a series of if statements that convert the text input to the pointer output based on input.

How do I do this?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
symtek
  • 51
  • 1
  • 8
  • 3
    Is this c or c++? the answers would not be the same, if it's c, then [don't cast `malloc()`](http://stackoverflow.com/a/605858/1983495) otherwise use the correct casting style `reinterpret_cast(malloc(sizeof(*A));` and also don't use `struct` if it's c++. – Iharob Al Asimi Mar 09 '15 at 17:13
  • 4
    The simple answer; You can't. Your variable name `A` doesn't actually exist, per-say, in the compiled program. You can however use `std::map` to store a mapping of a string name to a `node` value. – aruisdante Mar 09 '15 at 17:14
  • 7
    @iharob It's perfectly legitimate to use `struct` in C++. It's exactly equivalent to `class` except its default visibility is public rather than private. Aggregate POD's are almost always written as `struct` to save the `public` statement. – aruisdante Mar 09 '15 at 17:15
  • What type is `state`? – R Sahu Mar 09 '15 at 17:15
  • 2
    @aruisdante I think he is talking about repeating the keyword, which is not necessary in C++. – Baum mit Augen Mar 09 '15 at 17:16
  • @aruisdante I am not saying it's not but why to do it? – Iharob Al Asimi Mar 09 '15 at 17:23
  • @aruisdante, how would I use std::map for this? – symtek Mar 09 '15 at 17:24
  • The question is not how do you do this, it's **why would you do this?** @JamesBirch `std::map` is a c++ stl object. – Iharob Al Asimi Mar 09 '15 at 17:24
  • You'd probably want to include a char in your node struct to store the letter. Then you could look up the node by the letter when it was entered... or have a separate array that implements the mapping of letters to nodes. – cherno Mar 09 '15 at 17:26
  • 1
    @JamesBirch: You'd use a `map`, rather than named pointers, to map characters to nodes. But that's in C++ of course; in C, you'll need to write your own data structure to manage that mapping. Whichever language you're using, and whatever you do, you can't use variable names directly at run-time, since they only exist at compile time. You'll need some kind of run-time data structure. – Mike Seymour Mar 09 '15 at 17:27
  • @cherno, I have -- `typedef struct node{ char name; struct node *zero; struct node *one; };` ... [in initializer:] `A->zero = H; A->one = C; A->name = 'A';` – symtek Mar 09 '15 at 17:30
  • @iharob, I'm doing this so I don't write a specific if statement for every single node. Mapping might work well. – symtek Mar 09 '15 at 17:30
  • No that is not my question, my question is why do you want a program with that feature? – Iharob Al Asimi Mar 09 '15 at 17:37
  • @JamesBirch I'm not quite sure what your trying to do. Are you trying to build a linked list as characters are entered, or does the list exist before hand? – cherno Mar 09 '15 at 17:52

1 Answers1

2
#include <stdio.h>

struct node
{
   char buf[16];
};

int main() 
{
   node A;
   node B;
   node C;
   node  D;
   node  E;
   node  F;
   node  G;
   node  H;
   char  c;    
   node *nodes[] = {&A, &B, &C, &D, &E, &F, &G, &H};


   printf("enter character (Q) to quit: ");
   while ((scanf(" %c", &c) == 1) && (c != 'Q')) 
   {
      if (c >= 'A' || c<='H') {
         node* mynode = nodes[c - 'A'];  
         printf("mynode %p\n", mynode);
      } else {
         printf("Invalid\n");
      }
      printf("enter character (Q) to quit: ");
   }
   return 0;
}
Mustafa Ozturk
  • 812
  • 4
  • 19