Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer/struct syntax 1

Status
Not open for further replies.

Guest42

Programmer
Nov 9, 2005
28
US
Quick syntax question

If I have some field **go
and I say *go = malloc(sizeof(whatever))
If go is a field in struct v, how would I achieve the same thing? For example, "v->go = malloc" would be the same thing as go = malloc, but I need *go = malloc.
 
Code:
*(v->go) = malloc(sizeof(whatever))
 
Whoa! What the hell??
I tried this code, which I assumed would blow up since I'm de-referencing a NULL pointer, but it worked normally. Why isn't it blowing up?
Code:
#include <stdlib.h>

int main( void )
{
   char* p = NULL;
   int size = sizeof( *p );  /* Why no boom here? */

   p = malloc( 5 * size );
   return 0;
}
 
Because of sizeof operands never elaborated, only the expression type determined. So it's a correct construct (no pointer dereferencing).
 
In addition to ArkM's answer, I would like to say that sizeof is calculated at compile time, so your linked application would appear like

Code:
int size = 1;

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top