Like Tree4Likes

Beginner question about array, and how to return them.

This is a discussion on Beginner question about array, and how to return them. within the C Programming forums, part of the General Programming Boards category; I played today with all the combinations that exist in C to receive and return Arrays, in 1d and 2d, ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Beginner question about array, and how to return them.

    I played today with all the combinations that exist in C to receive and return Arrays, in 1d and 2d, there is so many (!).

    I was wondering is there any option to return a 1d array or 2d array from a function but in different method then a pointer?

    for example :
    Code:
    //in main  you can write :
    
    int arr [5] = {};
    
    
    func(arr);//and then send it like this :
    
    // then in func:
    void func1 (int arr[]){
    arr[3] = 1;
    
    ...
    }
    now let's say I want to write something like this :
    Code:
    <what to write here > func2try (int arr []){
    int foo [6] = {};
    return foo  
    
    }
    
    in main :
    arraymo[2] = {};
    arraymo = func2try ( arr ); <--wont work, it seem that no option to return to main and still be able to work with [] format, only pointer format , which means that you only see the first cell value.
    Am I right? or I am missing something ?

    Thanks
    Last edited by opCprog; 04-25-2011 at 07:09 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    What you get back in the second case is simply int*
    Likewise, the variable in main must be an int* as well.

    You can't return arrays by value, you can only get a pointer to the first element (like you do when you pass arrays as parameters).

    Also, when you do
    return foo;
    you're also returning a pointer to a local variable. The array you had goes out of scope, and main is left with an invalid pointer.

    Declaring
    static int foo[ ] = ...
    would resolve the scope issue.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Then what are my options when debugging

    Ok, then, if this is the case, what are my options if I want to debug the program?

    For example you said that I can only return by reference , that means that when I look in the debugger I can only see the first value that the pointer points to.
    How can I see all the array at once in the debugger? ( working with vs 2008)
    ( like when you use the [] declaration)
    Last edited by opCprog; 04-25-2011 at 08:00 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Read the debugger manual perhaps?

    There might be some syntax like
    print (int[10])ptr
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Quote Originally Posted by Salem View Post
    Read the debugger manual perhaps?

    There might be some syntax like
    print (int[10])ptr
    I assumed that VS 2008 ( and all the other version that exist ) are one of the most popular debug \ ide available.
    I thought that maybe someone will know, with out looking in the endless, and not user friendly manual .

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Well you can either spend time waiting for someone with that particular expertise to show up (not me, I don't program in windows), or you could spend some time reading the manual. Even if you don't find the answer, you might read and remember some other useful stuff for later.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Sam I really appreciate your technical help , but please don't lecture me on how to study.

    My time is limited, and I need to divide it efficiently .
    While the possibility of learning something new on the VS debugger manual (if I read all of it) is nice, I guess that I can make better progress in other tasks I have in that time.
    That's why I was writing my question in this forum to save this time. it is not out of laziness, as you try to describe it .

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,491
    Let me google that for you

    Forums should generally be your last resort, after you've scoured the web thoroughly. You clearly had a couple hours to wait around for an answer to fall from the sky, and argue with Salem about whether you're lazy, but couldn't scrape up 30 seconds for a Google search. Your time sounds real limited.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I don't agree, forums are for help period.
    This thread clearly state on the headline that it is a beginner question, if any of you think that it is a waste of your time, don't enter it or don't answer.

    Every thing in the world can be found in Google, usually that take time as well, most of the time it bring things that are not related to what you wanted, so you need to refine the search over and over, and it could take a lot more then few clicks. (sometimes hours)
    Even the link that you gave me isn't good, and doesn;t give a complete pro answer to what I ask.

    To enter a beginner thread (in a special forum that is built exactly for that questions\people ), and mock on ones knowledge by giving a link to Google isn't a fair game.

    And about the time, yes I can wait hour or so, until some professorial person will come and give a full complete answer, as this dilemma isn't bothering me so much, or interfering my work right now.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by opCprog View Post
    I don't agree, forums are for help period.
    So are help files.... which is more likely to be accurate?

    You were given the best advice possible... Read The eFing Manual... you should follow it.

    And this is why you should never use static arrays to fake "returning an array"...

    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,491
    Quote Originally Posted by opCprog View Post
    I don't agree, forums are for help period.
    I don't really care if you agree, it's just not the way we really work around here. You're coming to us for help, so you get to play by our rules. We expect some show of effort on your part, since we're here to help you learn, not to do all your work and thinking for you. If you want those services, you generally have to pay for them.

    This thread clearly state on the headline that it is a beginner question, if any of you think that it is a waste of your time, don't enter it or don't answer.
    Beginner questions are not a waste of my time. I'm here because I love programming and want to help other people learn to program. What is a waste of my time, however, is people who expect me to do a Google search for them.

    Every thing in the world can be found in Google, usually that take time as well, most of the time it bring things that are not related to what you wanted, so you need to refine the search over and over, and it could take a lot more then few clicks. (sometimes hours)
    Learning to use Google effectively is a great skill. It's not a gift from god that some people just "have". It can and should be cultivated, especially as a programmer. But it's a skill you will never have if somebody else does all your Googling for you. We don't expect you to spend hours looking for a simple answer, but we do expect at least a few minutes.

    Even the link that you gave me isn't good, and doesn;t give a complete pro answer to what I ask.
    I don't know what "a complete pro answer" would be, but that link was fine. It performed a Google search for you. The 2nd and 4th results of that search have exactly the info you need, very easy to find with just a few short minutes of reading. Hint: it involves the use of a comma.

    To enter a beginner thread (in a special forum that is built exactly for that questions\people ), and mock on ones knowledge by giving a link to Google isn't a fair game.
    It wasn't meant to mock your knowledge, just your laziness. Sounds fair to me.

    And about the time, yes I can wait hour or so, until some professorial person will come and give a full complete answer, as this dilemma isn't bothering me so much, or interfering my work right now.
    You're not winning any points with this one. If you can wait for an hour, you can spend a few minutes doing a Google search. Something like "I searched for X, and found this site, but don't really get what they mean by Y" goes a very long way in getting help from people. We don't really like the "I'm to lazy to try on my own, so please spoon feed me the answer since I don't actually want to learn anything", which is how you came across.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by opCprog View Post
    And about the time, yes I can wait hour or so, until some professorial person will come and give a full complete answer, as this dilemma isn't bothering me so much, or interfering my work right now.
    So, then, what you're saying is that you're miffed that someone who generally earns less than a school bus driver didn't take the time from his/her busy day to hand you ready to use code for free?

    Go find a dictionary --since you seem to have an aversion to Google-- and look up the word "selfish".
    Last edited by CommonTater; 04-25-2011 at 11:42 AM.
    mike65535 likes this.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    By the OP's statements, clearly someone's time is valuable. Ours is apparently not.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    By the OP's statements, clearly someone's time is valuable. Ours is apparently not.
    As they say: "No good deed goes unpunished."

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Unbelievable, you all wrote such long answer about , how to search in google( assuming I don't know what google is)
    Instead of answering simple question.

    All idea of forums is for people to share information , or help people.
    By time I mean that if anyone of you use vs 2008 in it's daily job or hobby, this kind of question will take he or she about 10 sec to answer and help.

    Did I ask anyone here to "hand me some code" ?
    Did I enforce anyone to answer?
    Did I sent anyone private msg and wasted or bugged anyone privately ?
    Did I wrote that my time is more value then anyone here?

    My question was simple. It seems you have a lot of time to lecture newbie people on how to learn, or work with google instead of simply help.

    Again, you don't have to answer, or enter this thread.
    Last edited by opCprog; 04-25-2011 at 01:08 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  2. Some Beginner/array Help.
    By Jimdubbs in forum C++ Programming
    Replies: 6
    Last Post: 07-23-2009, 03:27 AM
  3. beginner array help
    By chelpme in forum C Programming
    Replies: 5
    Last Post: 04-22-2009, 10:18 PM
  4. Array question - Beginner
    By INFERNO2K in forum C++ Programming
    Replies: 7
    Last Post: 11-10-2006, 10:03 AM
  5. beginner array question
    By X PaYnE X in forum C Programming
    Replies: 13
    Last Post: 01-09-2005, 11:03 AM

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