• C

adding an integer to a pointer while creating it (pointer).

Can someone please explain me what the folowing code is doing ? I have a problem at , why the pointer is being subtracted from the initial value while returning it. What does it achieve ? And the same (being added) while freeing it.

 * Allocates memory for an double vector from [beg...end]
double  *Dec_Vector(int beg, int end)
  double       *vec;
  if( (end - beg +1) <= 0)
     {printf("In Dec_Vector, Chk the parameters\n");exit(1); }

  vec = (double *)malloc((end-beg+1)*sizeof(double));
  if (vec == NULL) Abort("Error allocating memory in Dec_vector, Aborting ...\n");

 * Free a dec vector
void Free_Dec(double *vec, int beg, int end)
For a 2D array ....

 * Allocate memory for a double matrix [begrow...endrow][begcol...endcol]
double  **Dec_Matrix(int begrow, int endrow, int begcol, int endcol)
  double       **mat;
  int             i;

  mat = (double **)malloc( (endrow-begrow+1)*sizeof(double*));
  if (mat == NULL) Abort("Error allocating memory in Dec_Matrix, Aborting ...\n");
  mat -= begrow;
  for (i=begrow; i<=endrow; i++)
       mat[i] = (double *)malloc((endcol-begcol+1)*sizeof(double));
       if (mat[i] == NULL) Abort("Error allocating memory in Dec_Matrix, Aborting ...\n");
       mat[i] -= begcol;

 * Free a Dec matrix
void Free_Decmatrix(double **mat, int begrow, int endrow, int begcol, int endcol)
  int             i;

  for(i=begrow; i<=endrow; i++)


 And the array is referenced in the main program as below with their starting int values....
 *aa = Dec_Matrix(1,nclone,0,nprobe);
  for (i=1; i<=nclone; i++)
        for (j=0; j<=nprobe; j++)
    ----- here goes some code

 I  dont understand that why not just return the array without subtracting and indices references be just from 0 to its length...

thanks a lot for explaing this code snippet ..

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maybe this is done to use it by such way:

double* d = Dec_Vector(10, 20);

d[10] = 1.0;
d[11] = 2.0;
d[20] = 0.0;

If beg > 0, Dec_Vector returns pointer to incorrect memory location which should not be accessed directly. But using this pointer in index in  (beg, end) range is OK.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexFM is correct

as you know d[10] is equivalent to *(d+10)

it just forces you to access the vector/matrix elements by indices in the specified range
Hi code4susant,

Although I agree that this is a strange technique, there are some neat uses of it.

double v = Dec_Vector(-1,1);
int i;

for ( i = -1; i <= 1; i++ ) v[i] = ...;

See how it turns the sometimes unnatural need to index from 0 upwards to a much more natural walk through using a range that feels right?

Hi code4susant,

Better still, say you are coding a Tic-Tac-Toe game.

double  ** board = Dec_Matrix ( -1, 1, -1, 1 );

You can now use board[-1][-1] to access one corner and board[1][1] for the opposite corner. That feels far more natural to me that the two corners being board[0][0] and board[2][2]!

code4susantAuthor Commented:
Thank you all for such a prompt reply !  This is life at its best !

AflexFM hit the point instantly,hoomany spelled it out and thank you Paul for motivating its use . Overall , it was an excellent reply !

thanks again,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.