naming variables by parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flinks
    New Member
    • Aug 2007
    • 3

    naming variables by parameter

    Hi all,
    I'm starting in C and having problem with naming variables. I have this function:

    void new (int a)
    {
    int new_"$a";
    .
    .
    .
    }

    I'd like to replace "$a" with the content of variable a. It's possible to be done in C?

    Thank you for any help.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by flinks
    Hi all,
    I'm starting in C and having problem with naming variables. I have this function:

    void new (int a)
    {
    int new_"$a";
    .
    .
    .
    }

    I'd like to replace "$a" with the content of variable a. It's possible to be done in C?

    Thank you for any help.
    No it's not possible and anyway it would be pointless. You have declared new_??? as a stack variable so it only exists for the lifetime of the function call.

    Anyway neither C nore C++ support this sort of variable variables names (as would be available in some scripting languages (PHP springs to mind)).

    I think you might do better if you describe what you are trying to achieve then may be we could suggest a way to do it.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Since this is C and not C++, you couild check out the preprocessor token pasting operator ## and write a macro for this.

      Just don't do this in C++ as there are alternate ways to do this.

      Comment

      • flinks
        New Member
        • Aug 2007
        • 3

        #4
        Originally posted by Banfa
        No it's not possible and anyway it would be pointless. You have declared new_??? as a stack variable so it only exists for the lifetime of the function call.

        Anyway neither C nore C++ support this sort of variable variables names (as would be available in some scripting languages (PHP springs to mind)).

        I think you might do better if you describe what you are trying to achieve then may be we could suggest a way to do it.
        Thank you for reply.
        I thought in this because I write in bash, and it's is possible there.
        my problem is the follow:

        My program will receive a list of n number, depending on some tests it must be added to an existent linked list or then the program must create a new one to add then. I can do it of any way, but there is some considerations:
        I don't know how many numbers will enter;
        I don't know how many lists will be needed;
        I must use linked lists in the solution;

        I'll be very grateful if someone help me or indicate some material to I study.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          OK so you need a variable number of lists containing a variable number of numbers.

          It isn't nice which I would use a 2 layer linked list.

          That is I would have a linked list of structures that contained the criteria and the head pointer to the sublists of numbers.

          Of course if you where using C++ then I would suggest using the STL map and list classes.

          Do you know how to construct a basic linked list?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by weaknessforcats
            Since this is C and not C++, you couild check out the preprocessor token pasting operator ## and write a macro for this.
            Not at runtime, the preprocessor runs as part of the compiler.

            Comment

            • flinks
              New Member
              • Aug 2007
              • 3

              #7
              Originally posted by Banfa
              OK so you need a variable number of lists containing a variable number of numbers.

              It isn't nice which I would use a 2 layer linked list.

              That is I would have a linked list of structures that contained the criteria and the head pointer to the sublists of numbers.

              Of course if you where using C++ then I would suggest using the STL map and list classes.

              Do you know how to construct a basic linked list?
              I'm starting in linked lists today, but already can do the basic operations. I'll try to do the "nested" linked list, it seems possible. Thank you for the help.

              Comment

              Working...