Thread: Can I use a value of the variable as a name?

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Can I use a value of the variable as a name?

    Hi guys, I am making a program that should be able to make files/variables using a value of another variable...How do I do that? Here is what I have done so far.

    Code:
    #include <stdbool.h>                                                                                  
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    int i = 0;
    int a;
    
    struct pro
    {
    	int cmd; 
    	int property1;
    	};
    
    int np()
    {
    	scanf("%d", &a);
    	struct pro a //now here I have to make "a" the name so I can have this: "a.cmd", e.t.c. "34.cmd = 5";
    	return;
    	}
    
    int main()
    {
    
    	for(i=0;i<10;i++)
    	{
    		np();
    		}
    
    	return 0;	
    	}
    So I have a structure called "pro" and I need to struct a variable("struct pro something;") that has a name same as i inputed in variable's "a" value...
    Arduino rocks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Sounds like you need a different language.

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    So...no, eh?
    Arduino rocks!

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    Yeah 'no' is the answer. Since your 'a' variable is an int, why not just build an array of pointers to struct pro and index it that way?

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A better question might be: why do you think you need to do that?
    You could probably simulate it with a map-like structure.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It sounds like you want an an associative array. Many high level languages have this feature built in. In python you would use a dictionary.

    I implemented something like this once in C, but its quite tedious. The way I did it was to have a linked list of structs holding a string for the variable name, an integer enumerating the datatype, and a void pointer holding the data. To look up a variable i'd then run through the list using strcmp to find if it existed. Perhaps its not the most efficient way of doing it, but it worked. That said theres probably libraries around that can do this stuff better.

  7. #7
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    But, how can I make a file that has the name I inputed?
    Arduino rocks!

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    But, how can I make a file that has the name I inputed?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SUF ".txt"
    
    int main(void) {
    	char name[32];
    	
    	printf("Enter a name: ");
    	scanf("%27[a-zA-Z0-9_-]", name);
    	strcat(name,SUF);
    
    	fopen(name,"w");
    	
    	return 0;
    }
    If you want to attach a name to a struct, include it as a member:

    Code:
    struct mystruct {
          char name[32];
    };
    struct mystruct example;
    So the name can go in example.name ...if example were an array:
    Code:
    struct mystruct example[10];
    example[0].name can be different than example[1].name, etc.
    Last edited by MK27; 10-08-2009 at 09:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Cool, thanks! It works!
    Last edited by yann; 10-09-2009 at 03:39 AM.
    Arduino rocks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM