Thread: How to map a String onto a Variable

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    How to map a String onto a Variable

    I've got a pair of Windows sockets and I want the client to send certain variables to the server. The server should somehow tell the client which variables it shall send.
    I got the idea that the server could just send the name of the desired variable and the client should somehow map this string onto one of its variables but I don't know how to do it.
    Is this the right approach anyway, or do you have a better idea?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    How many variables?
    How many types of variables?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Quote Originally Posted by Salem View Post
    How many variables?
    How many types of variables?
    It first maybe 2-4 Variables, just to test the basic programm.

    Later up to 100.

    Mostly integers maybe som chars and booleans

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    How about
    Code:
    enum varType {
      isInt,
      isChar,
      isDouble,
    };
    
    struct varInfo {
      const char *name;
      enum varType type;
      void *ptr;
    };
    
    int magic;
    double money;
    char gender;
    
    
    struct varInfo info[] = {
      { "magic", isInt, &magic },
      { "money", isDouble, &money },
      { "gender", isChar, &gender },
    };
    On receipt of a string, you
    - search the array
    - if it's a match, cast a pointer of the appropriate type and send the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    Smile

    Quote Originally Posted by Salem View Post
    How about
    Code:
    enum varType {
      isInt,
      isChar,
      isDouble,
    };
    
    struct varInfo {
      const char *name;
      enum varType type;
      void *ptr;
    };
    
    int magic;
    double money;
    char gender;
    
    
    struct varInfo info[] = {
      { "magic", isInt, &magic },
      { "money", isDouble, &money },
      { "gender", isChar, &gender },
    };
    On receipt of a string, you
    - search the array
    - if it's a match, cast a pointer of the appropriate type and send the data.
    Thank you for this solution.
    It sounds good and I will test it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign string to the string variable inside structure!
    By king_zart in forum C++ Programming
    Replies: 20
    Last Post: 12-09-2012, 11:37 AM
  2. string as a variable name
    By violatro in forum C Programming
    Replies: 4
    Last Post: 06-01-2010, 01:01 AM
  3. Set string variable
    By Beaner in forum C Programming
    Replies: 3
    Last Post: 01-28-2006, 06:15 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. string variable used as a variable...
    By Supar in forum C++ Programming
    Replies: 13
    Last Post: 03-29-2003, 04:21 PM

Tags for this Thread