Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between these two?
C++
*returnColumnSizes=malloc(sizeof(int)*numRows);
 *(*returnColumnSizes+i)=i+1;// This statement is in loop

and

C++
*returnColumnSizes = (int*)malloc(sizeof(int)*numRows);
*(returnColumnSizes)[i]=i+1;//This statement is also in the loop same as first one


What I have tried:

There is so much confusion, I was unable to find the difference in that.
What is returnColumnSizes here?
Posted
Updated 23-Jun-21 3:56am
Comments
CPallini 23-Jun-21 10:04am    
"What is returnColumnSizes here"
What you have declared it to be.
_-_-_-me 23-Jun-21 10:16am    
Here returnColumnSizes is a double pointer .
It is like this:
int ** returnColumnSizes;
_-_-_-me 24-Jun-21 8:36am    
*returnColumnSizes = (int**)malloc(sizeof(int*) * numRows);
*(*returnColumnSizes+i)=i+1;// This statement is in loop

and

*returnColumnSizes = (int**)malloc(sizeof(int*) * numRows);
*(returnColumnSizes)[i]=i+1;//This statement is also in the loop same as first one

What is the difference between these two?
Thank you !

1 solution

I am guessing that returnColumnSizes is a pointer to an array of int types, based on the parameter passed to malloc. Not sure what the second lines are supposed to be doing, other than storing some value somewhere.
 
Share this answer
 
Comments
_-_-_-me 23-Jun-21 10:19am    
Thank you!
Here it is declared like this:
The declaration is this,
int **returnColumnSizes;
Richard MacCutchan 23-Jun-21 10:32am    
To be honest I find more than one level of indirection too much for my tiny brain. There are simpler ways of creating and using arrays in C.
Richard MacCutchan 23-Jun-21 10:39am    
Then I think it should really be:
*returnColumnSizes = (int**)malloc(sizeof(int*) * numRows); // see how spaces make things clearer?

But as I say, multiple levels of indirection - phooey!
jeron1 23-Jun-21 13:35pm    
I'd have to agree.
_-_-_-me 24-Jun-21 0:04am    
*returnColumnSizes = (int**)malloc(sizeof(int*) * numRows);
*(*returnColumnSizes+i)=i+1;// This statement is in loop

and

*returnColumnSizes = (int**)malloc(sizeof(int*) * numRows);
*(returnColumnSizes)[i]=i+1;//This statement is also in the loop same as first one

What is the difference between these two?
Thank you !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900