Thread: how can i call a func by touching some adrs ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    how can i call a func by touching some adrs ?

    Hello and good evening,...

    im famliear with an alternative way with commond block that instead of using if/case you use an array and just jump to next place in the array
    example:
    Code:
    void func(char *number)
    {
       int i = 0;
       int array[10]={0,1,2,3,4,5,6,7,8,9};
       for (i = 0; i < strlen(number) + 1; i++)
         printf( "%d" , array[number[i] - '0'] );
    }
    in this exapmle instead of using multipile if or case i get the value inside of an array.
    this nice only with known values.

    now i'd like to some how call function in this same method
    what i mean is some how to store a call func to other functions

    yours truley
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't understand your question, but you could write that function much more easier (and properly, since you're looping too far and reading the '\0').

    Code:
    void func(char *number)
    {
    	int i, iLength = strlen(number);
    	for (i = 0; i < iLength; i++)
    	{
    		printf("&#37;d",number[i] - '0');
    	}
    }
    Edit: Re-reading your post, I'm now assuming that your function was just an example of how to use arrays in that manner. What it now sounds like to me is that you might want to store function pointers inside the array and call them depending upon the index, but somehow I don't think that's what you mean.
    Last edited by MacGyver; 04-25-2007 at 01:20 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you trying to record an array index? If so, you'd just use the variable you're indexing the array with. You might want to pass that on to functions that need it, too.

    See the tutorials for more information, especially the C tutorials on arrays and functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I think you want an array of function pointers:
    Code:
    #include <stdio.h>
    
    
    typedef void (*f_t)( void );
    
    
    void f1( void ) { puts( "F1" ); }
    void f2( void ) { puts( "F2" ); }
    void f3( void ) { puts( "F3" ); }
    void f4( void ) { puts( "F4" ); }
    void f5( void ) { puts( "F5" ); }
    
    
    int main( void ) {
      f_t funcs[] = {
        f1,f2,f3,f4,f5
      };
      int i;
    
      for ( i = 0; i < 5; i++ ) {
        funcs[i]();
      }
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    the idea is not to find what number stores inside of it but to print some other value that have connection to it :
    if i would use complement to 10 //the example is based on this type.
    i would assign for the numbers:

    0 1 2 3 4 5 6 7 8 9 9
    ill use the next array
    Code:
    0 9 8 7 6 5 4 3 2 1 0
    so instead of using 10 case senetnce and on if i just use one simple for (with an if).

    here is cut from a func that i once wrote :for full code look here (char* foo::complement(char *first,char *second))//this func is mostly in c.
    here is quick cut (only what is important)

    Code:
    char  array[12]={'0','9','8','7','6','5','4','3','2','1','0','\0'};
    int i = 0;
    int flag = 0;
    for (i = smallest_size - 1;i > -1;i--)
    			{
    			
    				result[i + biggest_size - smallest_size]=array[ object.number[i] - '0' + flag];
    				if (result[i + biggest_size - smallest_size] != '0')
    					flag = 1;
    			}
    Last edited by jabka; 04-25-2007 at 01:40 PM. Reason: spelling
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    why didn' t i thout about creating pointer to func array..

    Quote Originally Posted by Noir View Post
    I think you want an array of function pointers:
    Code:
    #include <stdio.h>
    
    
    typedef void (*f_t)( void );
    
    
    void f1( void ) { puts( "F1" ); }
    void f2( void ) { puts( "F2" ); }
    void f3( void ) { puts( "F3" ); }
    void f4( void ) { puts( "F4" ); }
    void f5( void ) { puts( "F5" ); }
    
    
    int main( void ) {
      f_t funcs[] = {
        f1,f2,f3,f4,f5
      };
      int i;
    
      for ( i = 0; i < 5; i++ ) {
        funcs[i]();
      }
    
      return 0;
    }
    you got my idea .. that was exactly what i was going to ..
    thnx ..
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I still don't really get it, but it sounds like you might be looking for a structure. A structure is a way to tie data together. You could have an array of structures.
    Code:
    struct data_t {
        char name[80];
        int age;
    };
    
    struct data_t data[10];
    
    strcpy(data[0].name, "Joe");
    data[0].age = 10;
    
    strcpy(data[1].name, "Sam");
    data[1].age = 10;
    
    /* ... */
    
    int x;
    
    for(x = 0; x < 10; x ++) {
        puts(data[x].name);
    }
    [edit] Never mind. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Help needed with external call, WinExec() and timer.
    By algorithm in forum Windows Programming
    Replies: 9
    Last Post: 11-07-2001, 09:35 AM
  3. Recursive Functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-10-2001, 07:47 PM
  4. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM