Menu

Question about C in Dev

Sevko
2010-03-14
2012-09-26
  • Sevko

    Sevko - 2010-03-14

    Could some one tell me how do you pass arrays and string of arrays by
    reference as arguments in functions(in class we learned that theydon't need to
    be passed with pointers)

     
  • cpns

    cpns - 2010-03-15

    Well if in class you truly learned this, then you already know the answer!

    What are "strings of arrays" I wonder?

    In C passing by reference means exactly passing a pointer. C++ has a
    'reference' type qualifier which is semantically and syntactically different
    from a pointer; but you specified C, so that is not available to you.

    When you pass an array to a function, it implicitly 'degrades' to a pointer.
    This means that taking the sizeof the array within teh function yields the
    sizeof a pointer, not the size of the array. This occurs even if you use array
    syntax rather than pointer syntax to declare the argument, so generally the
    array syntax achieves nothing except a visual clue to the maintainer.
    Personally I avoid the syntax in favour of pointer; other contextual clues
    should make it obvious what the argument points to.

     
  • DARC

    DARC - 2010-03-15

    In C++ you either pass data or an address to data. When you pass by reference
    that means you pass an address to data. If your obj is data you must:
    1. pass the address of the object aka by reference "func(&myvari)" (note: &myvari is the address to data myvari)
    2. ceate a pointer, assign it to "myvari", and pass the pointer obj aka by reference "func(pointermyvari)" (note: pointermyvari is the address to data myvari)
    3. create a reference, assign it to "myvari", and pass the reference aka by reference "func(myref)" (note: myref is the address to data myvari)

    The other problem is that some objs in C++ are addresses to data and not data
    themselves. If the obj is an address already then just pass the obj
    "func(myobj)". Strings, char *, char are all addresses to data. As your
    classes advance you understand why. It has to do with the raw data elements
    the computer uses and how cpu/programs access them. C++ can be either very low
    level or very high level programming language. This is one of those low level
    things. In the case of strings, C++ has three solutions that all do the same
    thing.

    It is also a syntax issue if you use the wrong symbol with the wrong obj. This
    may be the root of your question. If so then your answer is that an array is
    an address to data. You pass it by reference by just naming it in your func
    parameters. Its already and address so do nothing to it. Learn what the
    asterick and ampersign mean in different parts of a statement or expression.
    It is not always the same and pointers are the most ironic. Its usage can be
    used three different ways depending on the symbol you use.

    Lastly, dont confuse a reference variable and passing by reference. You dont
    have to pass a reference variable to pass by reference. Simple right. To pass
    by reference you need an address period. keywords "address to data = pass by
    reference".See first paragraph on how to get that address. If your obj is
    already an address just pass it, and if its data gets its address.
    Happy programming

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.