Like Tree1Likes

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

This is a discussion on How can I let a function to read an array that is defined in my main function? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program to count how many solution there is in the Eight Queens puzzle, I defined an array ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    > 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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Make use of a function parameter
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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++.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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]
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    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 01:00 PM.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 3 123 LastLast
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, 12: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, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21