finding element width in dynamically allocated arrays

Discussion in 'C Programming' started by tricard, Jan 23, 2006.

  1. tricard

    tricard Guest

    Good day all,

    I have created a two dimensional array (matrix for my purposes) whose
    size is dynamically allocated. (i.e. rowSize and colSize are both taken
    as input, then malloc() is used to dynamically allocate the required
    memory). After the matrix is returned to main I want to pass it to a
    function, printMatrix() and have it displayed on screen. However, I do
    not want to send the rowSize and colSize arguments; instead I want to
    have the function identify the sizes. Is there any way to do this, or
    should I just send the rowSize and colSize as arguments?

    Thanks for the help

    Tim
     
    tricard, Jan 23, 2006
    #1
    1. Advertisements

  2. There's not portable way to do what you want to do. At least no way
    that I am aware of.

    Passing colsize and rowsize as arguments is an option, but it's error
    prone since you have to make sure that the three variables change
    accordingly. What I would do is define a struct like:

    struct dynamic_matrix {
    size_t rowsize;
    size_t colsize;
    base_type **matrix;
    }

    HTH
     
    Antonio Contreras, Jan 23, 2006
    #2
    1. Advertisements

  3. tricard

    tricard Guest

    That is exactly what I was moving towards :). Thanks for the help.

    Tim
     
    tricard, Jan 23, 2006
    #3
  4. tricard

    CBFalconer Guest

    Since an array is passed as a pointer to its first element, there
    is no way for a function to know the sizes unless specifically
    passed them. So you must pass those values.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    CBFalconer, Jan 23, 2006
    #4
  5. tricard

    Abhishek Guest

    Or probably you could do that this way..
    after u have assigned all the values in the matrix, you can assign a
    totally different value(A value u think which is definitely not there
    in the matrix) at the last position.
    Inside the function to which u pass just the address of the first
    element, you can scan through all the elements till you find the
    representing element. once you have found the element, you know how
    many elements it contains. but I am not sure if this technique can be
    used for all the dimensions of the array. But I am sure it will work
    for one dimension.
    probably someone else can add more to this.
    bye
     
    Abhishek, Jan 23, 2006
    #5
  6. Do what? Who? (certainly not Chuck)
    It's highly unlikely that anyone can add anything to what you just said,
    as it's not at all clear what are you talking about, and not even /who/
    are you replying to. Many won't even try.

    Also, using text-speak makes it /very/ difficult to read your post.

    Please quote what (and who!) you're replying to. If you use Google, use
    the instructions:

    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Vladimir S. Oka, Jan 23, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.