Topic : use of pointers - ST7- Ride

Forum : ST7/STM8

Original Post
Post Information Post
August 20, 2010 - 10:49am
Guest

Hi,
I'm making my first post here. It seems to be the best and fastest way to get feedback instead of using too many hours on the programming. :)

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.
I've found examples on this in my book (C++ from the ground up).

void swap(int *x, int *y){
Int temp;
Temp = *x;
*x=*y;
*y=temp;
}

But then I have to rewrite every function call to
swap(&Number1, &Number2);

Instead I would like to do it this way:
Void swap(int &x, int &y){
int temp;
temp=x;
x=y;
y=temp;
}
Where I can leave the function call as it is now.

However..... MY PROBLEM.....
I get this error message
*** ERROR C091 IN LINE 5 OF LEDs.h : Invalid parameter declaration
which is on the function declaration...

Is this related to the C/C++ differences, or am I doing something wrong, because the first exaple seems to compile without problems.
(And as everyone knows.... if it can compile without errors, there are no errors in the code ;) )

I hope someone can help be.
Best regards
Jacob

Replies
Post Information Post
+1
0
-1
August 20, 2010 - 11:41am
Raisonance Support Team

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

+1
0
-1
August 20, 2010 - 12:45pm
Guest

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

+1
0
-1
September 7, 2010 - 8:04am
Guest

It is correct: passing parameters by reference - and '&' as the "reference" operator - is strictly C++ only.

+1
0
-1
September 8, 2010 - 8:35am
Guest

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

+1
0
-1
September 9, 2010 - 10:26am
Guest

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

         int *pInt;    // Pointer to an int
  signed int *puInt;   // Pointer to a  signed int
unsigned int *psInt;   // Pointer to an unsigned int

In all cases, the value of the pointer is just an address