pointers
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! 
Re: pointers
The C99 standard allows for runtime 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. 
Re: pointers
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..... 
Re: pointers
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.

Re: pointers
Quote:

Re: pointers
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.

All times are GMT +5.5. The time now is 09:55. 