Assign a name to a pointer variable from another variable

Discussion in 'C Programming' started by Nmishra, May 22, 2004.

  1. Nmishra

    Nmishra Guest

    Hi,
    I have a problem in assigning a name to a pointer dynamically from
    another pointer.

    e.g. if I have

    char* name = "myPtr";

    and I want to have a int pointer having the name as myPtr taking the
    name from the variable name,

    int *(*name) = int[10]...
    this line of code gives me an error of type mismatch. I am using gcc
    as my compiler.

    and if i write as

    int *name = int[10] it takes name as the variable identifier rather
    than myPtr as the identifier.


    Any help is appreciated.

    Regards,
    NMishra
    Nmishra, May 22, 2004
    #1
    1. Advertising

  2. Nmishra

    Malcolm Guest

    "Nmishra" <> wrote in message
    > I have a problem in assigning a name to a pointer dynamically from
    > another pointer.
    >
    > e.g. if I have
    >
    > char* name = "myPtr";
    >
    > and I want to have a int pointer having the name as myPtr taking the
    > name from the variable name,
    >
    > int *(*name) = int[10]...
    > this line of code gives me an error of type mismatch. I am using gcc
    > as my compiler.
    >
    > and if i write as
    >
    > int *name = int[10] it takes name as the variable identifier rather
    > than myPtr as the identifier.
    >

    C variable names exist only at compile time

    void foo(void)
    {
    int alpha = 1;
    int beta = 2;
    char *vname = "alpha";
    int *ptr;

    /* totally illegal and impossible to work around */
    ptr = & thing_named_by_vname;
    }

    It is not possible to mix symbol names with runtime char arrays.
    Malcolm, May 23, 2004
    #2
    1. Advertising

  3. Nmishra

    Nmishra Guest

    Hi,
    thanks for the reply. I tried a lot and i could also make that out
    that its logically impossible to do that.

    thanks.


    "Malcolm" <> wrote in message news:<c8olve$l5a$>...
    > "Nmishra" <> wrote in message
    > > I have a problem in assigning a name to a pointer dynamically from
    > > another pointer.
    > >
    > > e.g. if I have
    > >
    > > char* name = "myPtr";
    > >
    > > and I want to have a int pointer having the name as myPtr taking the
    > > name from the variable name,
    > >
    > > int *(*name) = int[10]...
    > > this line of code gives me an error of type mismatch. I am using gcc
    > > as my compiler.
    > >
    > > and if i write as
    > >
    > > int *name = int[10] it takes name as the variable identifier rather
    > > than myPtr as the identifier.
    > >

    > C variable names exist only at compile time
    >
    > void foo(void)
    > {
    > int alpha = 1;
    > int beta = 2;
    > char *vname = "alpha";
    > int *ptr;
    >
    > /* totally illegal and impossible to work around */
    > ptr = & thing_named_by_vname;
    > }
    >
    > It is not possible to mix symbol names with runtime char arrays.
    Nmishra, May 23, 2004
    #3
  4. Nmishra

    Default User Guest

    Nmishra wrote:
    >
    > Hi,
    > I have a problem in assigning a name to a pointer dynamically from
    > another pointer.



    This is not something you can do, as Malcolm pointed out. So, why don't
    you tell us what problem you are trying to solve, rather than your
    broken solution to the problem. We can likely help.





    Brian Rodenborn
    Default User, May 24, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    8
    Views:
    53,566
    saravanavsk
    Jan 25, 2010
  2. Replies:
    18
    Views:
    7,075
    Karl Heinz Buchegger
    Jul 22, 2005
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,388
    Joe Smith
    Apr 22, 2007
  4. Terry
    Replies:
    3
    Views:
    110
    Terry
    Oct 15, 2003
  5. Replies:
    15
    Views:
    220
Loading...

Share This Page