Thread: How can I let a function to read an array that is defined in my main function?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    How can I let a function to read an array that is defined in my main function?

    I'm writing a program to count how many solution there is in the Eight Queens puzzle, I defined an array in the main function, and tried to use function to check them, but the array is not defined in the function. Is there a way that I can let the function to read the array in the main function?

    Thanks,
    Roger

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    > Is there a way that I can let the function to read the array in the main function?
    Sure... just pass the array along with its size to the function.(size nor required if it is an std::array)
    If you aren't sure about how to do that...read up more on function calls.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Make use of a function parameter
    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

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Sure... just pass the array along with its size to the function.(size nor required if it is an std::array)
    Passing size is evil! We can let the compiler do it for us:

    Code:
    #include <iostream>
    
    template<size_t N>
    void foo(int (&bar)[N])
    {
    	std::cout << N << std::endl;
    }
    
    int main()
    {
    	int bla[50];
    	foo(bla);
    }
    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.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Passing size is evil!
    Why ?

    Showing templates so soon is the fastest way to scare beginners away from C++.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And do you really want a specialized version of your function for each possible array size?
    What if the size is determined at runtime?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Why ?

    Showing templates so soon is the fastest way to scare beginners away from C++.
    Because beginners make mistakes. Like not passing in the proper correct size.
    It also lessens the need to manually pass in the size (which is errorprone) and have the compiler handle that job. We always want the compiler to handle as much stuff as possible.
    This template, I think, is hardly enough to scare people away. When you really get into template classes, that's where you scare people away, I think.

    Quote Originally Posted by CornedBee View Post
    And do you really want a specialized version of your function for each possible array size?
    What if the size is determined at runtime?
    As for the first, I don't see a problem unless you really find out it's a bottleneck.
    If the size is determined at runtime, you're better off using 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.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    We always want the compiler to handle as much stuff as possible.
    (Almost) Agreed ...but...
    You're starting to exhibit symptoms of the Steve Jobs Syndrome !

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why would you say such a thing?
    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.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Why would you say such a thing?
    *User doesn't need to know anything*
    *User isn't allowed to change anything*
    *Let (*this) handle everything...(and/or) take credit for it.*

    Analogous to
    [(>Why?)Because beginners make mistakes]
    Templates fix everything at compile time.
    [...want the compiler to handle as much stuff as possible]

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I dislike about Steve is that he takes away freedom from people and treat them like dumb idiots who needs to be put in place. I also dislike how they are abusing their position to use bully competitors.

    This is different from what symptoms I exhibit.
    I don't take away people their choice. They can do what they want. I provide a solution, perhaps not always the best. I never condone to robbing people the freedom of choosing solutions or tools.
    I don't treat people as things that need to be put in place. People make mistakes. Mistakes can cause grief, cost money or cause inconvenience to others. Therefore, I teach methods that minimize the amount of bugs that occur in code. This is not treating people as dumb; we all make mistakes, and the more we can abstract that, the better.
    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.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    Because beginners make mistakes. Like not passing in the proper correct size.
    It also lessens the need to manually pass in the size (which is errorprone) and have the compiler handle that job. We always want the compiler to handle as much stuff as possible.
    I'm not sure I understand. With respect to fixed size arrays:

    Code:
    const size_t mysize = sizeof somethings / sizeof somethings[0];
    /* or... */
    foo(somethings, sizeof somethings / sizeof somethings[0]);
    The compiler handles this code too, and I made it so it cannot be changed.

    Is the template really even necessary?
    Last edited by whiteflags; 12-01-2011 at 02:00 PM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I will admit that passing size that way (if you know how to) is not much more difficult or errorprone (unless you change variable name and forget to change that statement!).
    Still, it is something of an eyesore, you will have to admit.

    Instead you can shift it to your declaration and hide it from view (and also eliminates the possibilities of errors with passing in incorrect size).
    Mostly, using C-style arrays is just plain dumb unless there is a special reason for it. One should generally keep to the containers.

    So, to summarize: it is slightly less errorprone to do it that way and other than that, it's a matter of style.
    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.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I will admit that passing size that way (if you know how to) is not much more difficult or errorprone (unless you change variable name and forget to change that statement!).
    Still, it is something of an eyesore, you will have to admit.

    Instead you can shift it to your declaration and hide it from view (and also eliminates the possibilities of errors with passing in incorrect size).
    I don't change variable names that often but if I need to I would have my editor help me. I don't agree with bringing in a complex feature when there is something easier to use available. To wit, I'm not sure how much help it is when you have to explicitly instantiate the template. So really, saying that you've moved the problem to the template is the way to look at it.

    So, to summarize: it is slightly less errorprone to do it that way and other than that, it's a matter of style.
    I would say that considering a division statement to be an eyesore is a matter of your style.

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well... I use C Style arrays only in analogy to pointers (...otherwise..nothing beats std::vector or std::array !)
    That is why I brought up passing size in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-21-2011, 03:26 AM
  2. Passing array back to main function
    By greg677 in forum C Programming
    Replies: 11
    Last Post: 05-01-2010, 04:27 PM
  3. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  4. dynamically defined array size in a function
    By earth_angel in forum C Programming
    Replies: 21
    Last Post: 05-28-2005, 01:44 AM
  5. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM