Thread: Function input may char or char[] ..?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    Function input may char or char[] ..?

    Dear all , I have a function , and I need to use this function to deal with char and char[] , is this the idea of template ? and if so , how can I distinguish if the input is char or char[] ?

    thanks
    Last edited by GSalah; 11-15-2006 at 02:49 AM.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    void foo(int a) {}
    void foo(double a) {} //correct, the two foo()s are different in argument type
    void foo(char a) {} //still correct
    void foo(char *a) {} //acceptable
    void foo(my_class a) {} //any type will do
    int foo(my_class a) {} //error, return types don't count
    Just so you know, templates are a rather different kind of animal.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    It sounds like you're describing a need for function overloading.

    Code:
    #include <stdio.h>
    
    void process (char c) {
      printf ("Handling a char: %c\n", c);
    }
    
    void process (char c[]) {
      printf ("Handling an array: %s\n", c);
    }
    
    int main (void) {
      char letter='A';
      char name[] = "James";
    
      process(letter);
      process(name);
    
      return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM