Thread: Passing 2d char arrays to function??

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Hey, i was hoping if anybody could tell me how to pass 2d char arrays to a function. I dont even know if this is possible or not. So thankss if you do help!

    chris D.

    i think i should say more, so im trying to pass a 2d char array from "int main()" to a function, but all the ways i try it gives me tons of errors
    Last edited by chris3545; 03-14-2011 at 02:29 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using char arrays instead of std::string?
    Furthermore, since you are using an array of strings, why not use std::vector?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you for replying, i dont know what "std::vectors" are so i think a string would be good for what im doing. so how would i pass a 2d string array to function??

    thanks

    if anyone can help please do, i need this done tonight, im been going though many books and i cant figure it out!?!
    Last edited by chris3545; 03-14-2011 at 04:09 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Copy/paste.

    Code:
    int main ( ) {
      char arr[10][20];
      foo(arr);
    }
    So what does foo look like?

    Simple, you copy/paste the declaration of your array.
    Code:
    void foo ( char arr[10][20] ) {
      // do stuff
    }
    Some compilers MAY complain about the useless major dimension, in which case a small edit gets you to
    Code:
    void foo ( char arr[][20] ) {
      // do stuff
    }
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I was suggesting was this:
    Code:
    void foo(const std::vector<std::string>& mystrings)
    {
    	for (auto it = mystrings.begin(), end = mystrings.end(); it != end; ++it)
    		std::cout << *it << " ";
    }
    
    int main()
    {
    	std::vector<std::string> mystrings;
    	mystrings.push_back("Hello");
    	mystrings.push_back("World!");
    	foo(mystrings);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you guys so much! this help me out alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  2. Passing 2d arrays to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2005, 09:30 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM