pointers

Discussion in 'C' started by sunil_ghai, Apr 14, 2007.

  1. sunil_ghai

    sunil_ghai New Member

    Joined:
    Apr 14, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    members plz help me..
    while defining 2D array through pointers we must know the maximum limit of one expression like:
    int *ptr[x],here x should be known and we can further allocate the memory through malloc function.
    can anyone plz tell me how can we define a pointer in which both limits can be entered by user?
    thanks in advance!
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The C99 standard allows for run-time assignment of array dimensions, but few compilers are compliant in that respect. Consequently, we fake it. Suppose we want a 2D array of dimensions m x n. We ask malloc to give us memory for m pointers to the element type. We then ask malloc to give us memory for n elements. We store the pointer to that memory in the first array. We then repeat that second request until we have done it m times, storing each of those pointers in the first array.

    This gives us m pointers to arrays containing n elements. Job done. Understand that this is not a true 2D array, in that it is not all contiguous, but it gives us a means of addressing any element, given a value for m and a value for n.

    Take a shot at implementing that. If you have problems, please post back.
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    int **p;
    p=(int **)malloc(m*n*sizeof(int));
    m and n are the rows and columns .....
    this is how you have to allocate memory to 2D array. No one max limit is needed.....
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Wrecker, that is not correct. In your code, p is a pointer to an integer pointer. Dereferencing it will provide a value that is expected to be a pointer to an integer, not an integer. One can allocate the entire array in one swoop, as you show, but one can not then access it in 2D fashion, except by performing arithmetic manipulations involving multiplications by m.
     
  5. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Hey DaWei, actually the code posted works very fine and to tell you that this was the code used when i had won the national level programming competition. Also i didnot understaand why cant we access it in 2D fashin. I dont mean to say that M the only one who is correct but To the best of my knowledge m correct and code also......
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I invite you to write your code and add a line where you attempt to store an integer at p[m][n] (that would be 2D array notation) for some valid value of m and n.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice