Forum : ST7/STM8
Post Information | Post |
---|---|
August 20, 2010 - 10:49am
|
Hi, I'm doing a program for the ST72F321 processor, and I've come across some speed/time issues, that I'll try to eliminate by using pointers. I'm not the biggest user of pointers, since I've not had the timelimits on execution as I have now. I'm trying to rewrite some functions to take advantage of pointers instead of sending big variables back and forth in the SW. I seem to have 2 options, either call the function with a pointer to the value, or to the address. void swap(int *x, int *y){ But then I have to rewrite every function call to Instead I would like to do it this way: However..... MY PROBLEM..... Is this related to the C/C++ differences, or am I doing something wrong, because the first exaple seems to compile without problems. I hope someone can help be. |
Hi,
This will have to be confirmed by our C standard experts who are on vacation right now, but I think that as you guessed, your second syntax is valid in C++ but not in C.
Anyways, I strongly recommend against using this syntax, even in C++, as it is very bad for code readability, which has a big impact on debugging, maintenance, etc. My personal experience is that using this syntax has been the source of many mistakes.
Best Regards,
Vincent
Thanks for the answer.
I'll put the idea on the shelf for a while, maybe until the experts returns from vacation, and do it as the first example.
As long as it's faster than the current code, I'll be happy. :)
Best regards
Jacob
It is correct: passing parameters by reference - and '&' as the "reference" operator - is strictly C++ only.
thanks for the answer.
Just to make sure, the first way/example I posted is ok for C?
And just to be sure of this, since I haven't done much in pointers before:
I have a variable, e.g.
signed int Number;
I want to create a pointer for this one, do I then have to make that a:
signed int PtrNumber;
or
unsigned int PtrNumber;
and after this:
*PtrNumber = Number;
As I've understood, the PtrNumber is the address of the Number now, and I've never heard about a signed address before ;)
So question is realy: should a pointer always be an unsigned of same kind as the variable pointed too?
Best regards
Jacob
This is 'C' textbook stuff!
The type specification that you give in a pointer declaration is the type of the pointer's target; ie, the type that the pointer points to
In all cases, the value of the pointer is just an address