DIY sysex fader box status and questions.

ToAd tom.adam at thebigear.be
Tue May 1 23:24:08 CEST 2012


THIS   NEEDS   STUDYING.....

Thanks Peter I'll look into this!
Cheers,
ToAd
On 1/05/2012 22:45, Peter Korsten wrote:
> Hi Tom,
>
> If you want to pass a variable by reference instead of by value in C, 
> you can't really do that. What you have to do is pass a pointer to the 
> variable, instead of the variable itself.
>
> Consider that you have this function:
>
> void increase( int a )
> {
>     a++;
> }
>
> int main( int argc, char **argv )
> {
>     int b = 1;
>     increase( b );
>     printf( "b=%d\n", b );
>     return 0;
> }
>
> This won't do any good. What you have to do instead is this:
>
> void increase( int *a )
> {
>     (*a)++;
> }
>
> The parentheses are not really necessary, but they're there to make it 
> easier for you to see what's going on. You invoke it as follows:
>
> int main( int argc, char **argv )
> {
>     int b = 1;
>     increase( &b );
>     printf( "b=%d\n", b );
>     return 0;
> }
>
> You see the use of the '*' and '&'. In the function declaration, the 
> '*' means 'pointer to', in this case a pointer to an int. In the 
> '(*a)++' statement, the '*' means 'the contents of the memory address 
> pointed to by "a"'. The '&' operator means 'the address of the variable'.
>
> It's essential to understand that the variable 'a' in the 'increase' 
> function is NOT an integer, but a POINTER TO an integer. The pointer 
> needs to point to something, otherwise it's meaningless. You could do 
> the following:
>
>     increase( NULL );
>
> and get a segmentation violation, if your CPU has an MMU. Otherwise, 
> you'll be trashing your memory.
>
> Now, how about arrays? Well, there's no real difference between arrays 
> and pointers in C, so you're in luck. To pass an array to a function, 
> you simply pass the array variable. For example:
>
> void increase( int *a, int size )
> {
>     int i;
>     for( i = 0; i < size; i++ )
>         a[i]++;
> }
>
> int main( int argc, char **argv )
> {
>     int b[] = { 0, 1, 2, 3 };
>     increase( b, sizeof b / sizeof (int) );
>     for( i = 0; i < sizeof b / sizeof (int); i++ )
>         printf( "b[%d]=%d\n", i, b[i] );
>     return 0;
> }
>
> (Please note that I'm typing this from heart and haven't actually 
> tested the code, but it should be OK.)
>
> Knowing pointers is the key to understanding C, but it may take you a 
> while to wrap your head around them.
>
> - Peter
> _______________________________________________
> music-bar mailing list
> music-bar at lists.music-bar.org
> http://lists.music-bar.org/cgi-bin/mailman/listinfo/music-bar



More information about the music-bar mailing list