Re: [Vala] What is the right syntax for defining pointers or references or "aliases" in Vala ?



21.06.2011 01:26, Serge Hulne пишет:
All right , they might "behave like" pointers, but they are not mere
pointers, cf:


///
using Posix;

void main (string[] argv) {

    string a = "hello";
    string b = a;
    Posix.stdout.printf("a = %p\n", &a);
    Posix.stdout.printf("b = %p\n", &b);
}
///

Which yields:

a = 0x7fff5fbff890
b = 0x7fff5fbff880

Obviously two different addresses (pointer values).

Also changing b does not change a.

This is very confusing, from a C (or C++) point of view !

The Vala references, look more like Python (implicit) references than
like C (explicit) references.

Obviously, they are not ordinary C pointers as in :

///
char * a = "Hello";
char * b = a;
///

Serge.


What am I doing wrong?

///
using Posix;

void main (string[] argv) {

    string a = "hello";
    string* b = a;

    stdout.printf("a = %p\n", a);
    stdout.printf("b = %p\n", b);

    strcpy(b, "bye");

     stdout.printf("\na = %s\n", a);
}
///

The output is:

///
a = 0x1d55010
b = 0x1d55010

a = bye
///

Isn't it exactly what you expected? b is a pointer to a, changing b
changes a.

By the way, as pointed before, Vala's 'unowned' keyword makes variable
to be a pointer, Ex.:

///
using Posix;

void main (string[] argv) {

    string a = "hello";
    unowned string b = a;

    stdout.printf("a = %p\n", a);
    stdout.printf("b = %p\n", b);

    strcpy(b, "bye");

     stdout.printf("\na = %s\n", a);
}
///

This will work exactly the same.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]