dynamic declaration of variables

This is a discussion on dynamic declaration of variables within the C Programming forums, part of the General Programming Boards category; hi, my second question of the day so what i'm now trying to do is to dynamically declare a variable ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    dynamic declaration of variables

    hi, my second question of the day

    so what i'm now trying to do is to dynamically declare a variable something like this:

    Code:
    void *myFunction(char *type){
          
           if (*type == 'S'){
    		SSeq *string = NULL;
    	}
    	else if (*type == 'Q'){
    		QSeq *string = NULL;
    	}
    
    // Here i would like for my string to be either SSeq of QSeq.
    Is something like tjis even possible in c ???

    thank you

    baxy

    PS

    SSeq and QSeq are both structures defined in header file

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by baxy77bax View Post
    // Here i would like for my string to be either SSeq of QSeq.
    [/code]

    Is something like tjis even possible in c ???
    It actually is under C99, but then the variable goes out of scope, if you see what I mean:

    Code:
           if (*type == 'S'){
    		SSeq *string = NULL;
    	}
    	else if (*type == 'Q'){
    		QSeq *string = NULL;
    	}
    
    // Here is where "string" does not exist, since it was declared 
    //in a block scope and we are not inside the block anymore
    So kind of useless. Depending on what you are trying to do, you could accomplish the same thing with a void pointer and some casting.
    Last edited by MK27; 04-04-2011 at 11:15 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

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    both variables undergo the same procedure but they are loaded from Subject / Query file ... so i'm trying to avoid to write the same function twice. so every step is the same. therefore i want to call the function once and tell it to fill QSeq and in some other part of the code call the function to fill SSeq structure so that in the end i have query data in QSeq structure and subject data in the SSeq structure

    thanx
    Last edited by baxy77bax; 04-04-2011 at 11:17 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why wouldn't you just pass in the actual string...
    Code:
    void *myFunction(void *Str){
          free(Str);      
          return NULL; }
    
    //..... then call it as...
    
    SSeq = myFunction(SSeq);
    QSeq = myFunction(QSeq);
    I know it's only an example for concept... but if you return NULL or NULL the pointer without freeing the memory you're creating a memory leak.
    Last edited by CommonTater; 04-04-2011 at 11:22 AM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    You know every types of pointers are the same -- they are all unsigned integers. So you can convert them to whatever types you want. And using void* seems better here.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Thanx,

    that helped.... I think I'm tired and thinking properly any more .... it is working now


    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  2. Replies: 6
    Last Post: 08-23-2008, 02:16 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. errors initializing D3D
    By Vacation Guy in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2005, 01:20 PM
  5. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2004, 12:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21