Thread: how can i pass this array to my function?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    how can i pass this array to my function?

    Hello everyone,

    I'm given the following main() function and is required to write a function to do things. Could someone please help me with the prototype? Im really stuck and dont know how to pass a string parameter to MyFunc. Thanks in advance!

    Code:
    int main()
    {
        char MyString[2][5] = {"1234", "abc5"};
        
        for(i=0;i<2;i++)
             MyFunc(MyString[i]);
       
        return 0;
    }
    
    void MyFunc( what_should_i_put_here )
    {
        search throw MyString ("1234" and "abc5") to do stuff;
    }

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Try this. I am not sure how well it will work.
    Code:
    void MyFunc(char strings[][]){
              /*Code Here*/
    }
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps this.
    Code:
    #include <stdio.h>
    
    void MyFunc( char *text )
    {
       puts(text);
    }
    
    int main()
    {
        char MyString[2][5] = {"1234", "abc5"};
        int i;
        for ( i = 0; i < 2; ++i )
        {
           MyFunc(MyString[i]);
        }
        return 0;
    }
    
    /* my output
    1234
    abc5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    void MyFunc( char MyString[2][5] )
    works too.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Quote Originally Posted by Dave_Sinkula
    Perhaps this.
    Code:
    #include <stdio.h>
    
    void MyFunc( char *text )
    {
       puts(text);
    }
    
    ....
    
    /* my output
    1234
    abc5
    */
    Thanks so much . This works perfectly ...Here is what i needed to to (main function is given).

    Code:
    #include <stdio.h>
    
    void MyFNatoi(char *, int *);
    
    int main(void)
    {
        char numbers[5][10] = { "123456789", "0001234", "abc5xyz", "012a", "987654321" };
        int i, value;
    
        for(i=0; i<5; i++)
        {
            MyFNatoi(numbers[i], &value);
            printf("array: '%s' int value: %d\n", numbers[i], value);
        }
    
        return (0);
    }
    
    void MyFNatoi(char *a, int *value)
    {
        int number;
        int temp;
        int i=0;
    
        while(a[i]=='0')
            i++;
    
        do
        {
                temp=a[i]-'0';
                if(temp>=0 && temp<10)
                {
                    number=number*10+temp;
                    i++;
                }
                else
                    number=0;
        }while( number!=0 && a[i]!='\0' );
    
        *value=number;
    }
    Output:
    array: '123456789' int value: 123456789
    array: '0001234' int value: 1234
    array: 'abc5xyz' int value: 0
    array: '012a' int value: 0
    array: '987654321' int value: 987654321
    my task was just to write a function to convert character array to integer. Some more examples:
    Code:
    character array ('s'):     integer pointer (*value):
       ----------------------------------------------------
       "abc123"                   0
       "49"                      49
       "678abc"                   0
       ""                        0
       "889123"                   889123
    Once again, thanks so much . That was quick replies .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Is it possible to pass an array to a function?
    By Loic in forum C++ Programming
    Replies: 20
    Last Post: 05-06-2007, 10:04 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM