Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was confused with following syntax, but that I read a bit about it, I think it is valid:

C++
typedef struct t_config
{
	u16_t   x;  
	u16_t   y; 
	u16_t   z_pdp;
} t_config, *pt_config; // What is *pt_config?? Is it a pointer? Or a type?

 
void profile_append(pt_config conf); // If what is written above is a variable, then what is pt_config??? It is written as a type here
Posted
Updated 4-Jul-13 4:21am
v3

This definition means that t_config is a struct, and pt_config is a pointer to that struct.

The following are equivalent declarations:
void profile_append(pt_config conf);
void profile_append(t_config conf*);
 
Share this answer
 
int p;      // p is variable of int type
int *pNo;  // pNo is a pointer variable which will point to int type


In similar ways *pt_config means pointer to t_config type which is structure i.e. user define data type
 
Share this answer
 
As it is defined with a typedef, pt_config is a type definition. Think of it as
C++
typdef   struct t_config*   pt_config;

From now on you can use pt_config in all places where a type is expected, for example in a function argument list:
C++
void profile_append(pt_config conf);

The above declaration is the same as:
C++
void profile_append(struct t_config* conf);
 
Share this answer
 
The typedef statement defines the structure as a type that can be used elsewhere in your code. In essence it provides a shorthand notation, so you can use t_config as a structure reference and pt_config as a pointer to that structure type. The function definition below is saying that this function takes a pointer to a variable of the structure type as its only parameter. The conf variable name is merely a place holder and may be replaced with any name in your actual code. So you could write something like:
C++
t_config myConfig;              // allocate a t_config structure on the stack
myConfig.x = 0;                 // }
myConfig.y = 0;                 //  } initialise the structure's variables
myConfig.z_pdp = 100;           // }
}
void profile_append(&MYconfiG); // pass the address of (pointer to) the structure

// or

pt_config pMyConfig = new t_config; // allocate a new t_config structure on the heap 
profile_append(pMyConfig);          // pass the pointer to the structure
// maybe profile append will fill in the values ...
 
Share this answer
 

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