Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- From: "Giovanni Dicanio" <giovanniDOTdicanio@xxxxxxxxxxxxxxxxx>
- Date: Sat, 11 Oct 2008 22:29:55 +0200
One more note:
This is a notation suggestion, that may help you with pointers in C/C++ (I
found that useful myself).
When you have a pointer to something, you can prefix the variable name with
a 'p':
MYDATA MyData;
MYDATA * pMyData; /* pointer to MYDATA */
Now, there is a "cancelation rule": the * and the 'p' cancel each other,
e.g.:
*pMyData
you can see the * and 'p', so they cancel, and *pMyData is kind of MyData
(i.e. *pMyData is of type MYDATA, it is not a pointer anymore).
This comes in handy especially when you have double indirection pointers.
e.g.
void DoSomething( MYDATA ** ppMyData ); /* double pointer --> 2 'p's
prefix */
Now:
- ppMyData is a double indirection pointer to MYDATA (i.e. MYDATA **)
- *ppMyData -> * and the first 'p' cancel, so this is like pMyData, i.e. a
pointer (simple pointer) to MYDATA (i.e. MYDATA *)
- **ppMyData -> both * and 'p's cancel, so this is like MyData, i.e. type
MYDATA (no more pointer).
If you use this notation consistently, I think that this may help in your
code.
e.g.
void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );
Change to:
void config(
...
PASSCODE ** ppPassCode,
... );
Your wrong code was:
obj_PASSCODE = malloc (sizeof (PASSCODE));
Now, you see, malloc() is expected to return a pointer to PASSCODE
structure, so malloc() is expected to return a PASSCODE*, right?
You have ppPassCode as function parameter; how can you get a PASSCODE* from
ppPassCode?
Simple: cancel one 'p', using one '*':
*ppPassCode
is of type PASSCODE*, because * and the first 'p' cancel, and you have
pPassCode (i.e. PASSCODE *).
I hope I did not make much confusion with my English... But, trust me, this
rule is very simple, and after some practice you may find it helpful.
Giovanni
.
- Follow-Ups:
- References:
- Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- From: Robby
- Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- From: Giovanni Dicanio
- Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- Prev by Date: Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- Next by Date: Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- Previous by thread: Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- Next by thread: Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!
- Index(es):
Relevant Pages
|