Thread: Passing array to another function

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Passing array to another function

    Hi guys, just want to verify about passing an array to another function.

    lets assume I've an array
    Code:
    int arr1[5]
    and I want to pass it to another function, is it enough to just pass the pointer of it "arr1" ?
    more over I'm curious if I could also pass instead of "arr1" , "arr1+3" then the other function gets as input pointer "arr1+3" .. can I then inside the other function do any manipulation on array "arr1" starting from arr1 and not from arr1+3? meaning for instance yes I have in the other function the pointer starting from arr1+3 , I can inside the other function do a subtraction of it (arr1+3)-3 then I would get the pointer arr1 which is the array itself !, in other words whenever I have pointer to array doesn't matter from which elements it points .. then I can do manipulation on the whole array by just having one of the array "pointers"(meaning any pointer of the array that points on element).

    to sum up, pointers in C not just acting as pointers sometimes acting like elements itself for example in strings the pointer is also a string itself.... am I right?!


    thanks beforehand !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    is it enough to just pass the pointer of it "arr1" ?
    Maybe. Generally, you should also pick one:
    • Pass a length/size argument.
    • Pass another pointer to one-past-the-end of the array/range.
    • Have a known constant size for the array.
    • Designate a sentinel value to denote the end of the elements of the array that are in use.

    The first two tend to be safe and yet flexible. The third tends to be safe, but inflexible. The fourth can be safe if carefully handled, but the decades of buffer overflow vulnerabilities with null-terminated strings indicates that it may be a rather optimistic proposition.

    Quote Originally Posted by RyanC
    more over I'm curious if I could also pass instead of "arr1" , "arr1+3" then the other function gets as input pointer "arr1+3" .. can I then inside the other function do any manipulation on array "arr1" starting from arr1 and not from arr1+3?
    Yes, but that would be poor practice since the function wouldn't know that there are three elements before the element that that pointer points to (unless it is designed to iterate in reverse, or something like that). Therefore, trying to do that sort of thing is likely to be error-prone: you could end up trying to access an element that doesn't exist.
    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

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Thanks alot mayne!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a 2D array to a function
    By zahid990170 in forum C Programming
    Replies: 7
    Last Post: 09-17-2013, 10:50 AM
  2. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  3. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  4. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM
  5. 2D array passing to function
    By mkorolen in forum C Programming
    Replies: 5
    Last Post: 03-31-2002, 08:37 PM

Tags for this Thread