Thread: How To pass 2 dimensional array of strings to a function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    How To pass 2 dimensional array of strings to a function

    Hello,
    How To pass 2 dimensional array of strings to a function??at a go?
    means i dont want to pass address of each string 1 by 1..but want to pass whole 2D array string at a go?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    and ya those strings are local to main() not the global ones

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chottachatri View Post
    Hello,
    How To pass 2 dimensional array of strings to a function??at a go?
    means i dont want to pass address of each string 1 by 1..but want to pass whole 2D array string at a go?
    Maybe it's too early for me, but I don't see why you don't just pass the base address of the array, to the function.

    What's this "pass address of each string 1 by 1", stuff about?

    Would you show the relevant code in a post and surround it with the forum's code tags, please?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    This is what i mean passing address of each string 1 by 1

    Code:
    #include <string.h>
    #include <stdio.h>
    void function(char *temp)
    {
    printf("&#37;s\n",temp);
    
    }
    void main()
    {
    int i,n;
    char temp[10][30];
    printf("\n\nHow Many Names Do You Want To Enter :");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n\nEnter Name ->%d :",i+1);
      flushall();
      gets(temp[i]);
    }
    for(i=0;i<n;i++)
    {
     function(temp[i]);
    }
    getch();
    }
    Look at the above code each string is passed 1 by 1 in loop but i dont want to do that instead of loop i just want to pass the address a single time. How is it possible?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might write the function as:
    Code:
    void print_strings(char arr[][30], size_t size)
    {
        size_t i;
        for (i = 0; i < size; ++i)
        {
            printf("&#37;s\n", arr[i]);
        }
    }
    You would then call it as:
    Code:
    print_strings(temp, 10);
    Incidentally, please read our wiki FAQ concerning void main and [url=http://cpwiki.sourceforge.net/Gets]gets[/man] to find out what is wrong with them. You should also indent your code better.
    Last edited by laserlight; 01-23-2008 at 07:03 AM. Reason: Corrected the example code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    do this:
    Code:
    void func(char** names, n)
    {
    for (int i=0; i<n; i++) puts(names[i]);
    }
    
    // then call like this:
    func(temp, n);

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    duh! laserlight is, fast like laser! fast like light!

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably becasu temp is declared as char temp[10][30]
    print_strings should be declared as
    Code:
    void print_strings(char arr[][30], size_t size);
    and not char**

    PS. And i should be declared as size_t not int
    Last edited by vart; 01-23-2008 at 07:00 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok laserlight thank you very much! excellent! and ya i will use int main from now onwards

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    print_strings should be declared as

    And i should be declared as size_t not int
    Indeed, I overlooked both, so I shall make the fixes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok now can anybody tell me how to do the same job with integer array??i mean please can anybody give me code for that? i have done it by 1 method but anybody knows a sweet and short method for it then please tell me?

    Code:
    #include <stdio.h>
    void print(int *a,int m,int n)
    {
     int i,j;
     for(i=0;i<m;i++)
     {
       for(j=0;j<n;j++)
       {
         printf("&#37;d ",*(a+i*n+j));
       }
         printf("\n");
     }
    
    }
    
    int main(void)
    {
     int a[3][3],i,k=1,j;
     for(i=0;i<3;i++)
     {
       for(j=0;j<3;j++)
       {
         a[i][j]=k++;
       }
     }
     print(&a[0][0],3,3);
     return 0;
    }
    Also explain me the funtion of size_t i haven't come across that function
    Last edited by chottachatri; 01-23-2008 at 07:23 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you base your new code on my corrected example, you should have concluded that:
    Code:
    void print(int *a,int m,int n)
    should be:
    Code:
    void print(int a[][3],int m,int n)
    and that:
    Code:
    print(&a[0][0],3,3);
    should be:
    Code:
    print(a, 3, 3);
    Also, you can use a normal array subscript with a[i][j] instead of trying to compute the array index yourself.

    By the way, I think that an indentation of just one space may be too little. Typically about four spaces would be optimal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    one dimention parameter is enough - second is hardcoded
    Code:
    void print(int a[][3],int m)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    do this:
    Code:
    void func(char** names, n)
    {
    for (int i=0; i<n; i++) puts(names[i]);
    }
    
    // then call like this:
    func(temp, n);
    A double pointer won't work as a replacement for a 2D array - it only works the other way around, that you can make a 2D array by using a double pointer. If you are going to use a double pointer, you will have to allocate "rows" number of pointers, then assign each pointer to the first element of the rows.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but anybody knows a sweet and short method for it then please tell me?
    Yeah, it's called copy and paste.

    If this is your array,
    int arr[2][3][4];

    Then this is your function prototype
    void func ( int arr[2][3][4] );


    Yes, you can then fiddle with the prototype to change it into either of these forms, but you don't buy a lot. Some compilers will warn about the left-most dimension not being empty, but apart from that, it's good.
    Alternatives:
    void func ( int arr[][3][4] );
    void func ( int (*arr)[3][4] );


    Irrespective of the form you choose to write the function prototype / declaration, it would be called with
    func ( arr );

    And the usual array access subscripts you would use if the array were in scope will still do what you want inside the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. receiving function value which returns array of strings
    By saeed144 in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 07:54 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM