www.digitalmars.com         C & C++   DMDScript  

D - twinkle twinkle little stars

reply Lewis <dethbomb hotmail.com> writes:
One of the most confusing things for me in D or any C style language (curly 
braces and such) is the use of stars * here and there and what there placement 
means...

for example i know that *MyString would indicate a pointer to MyString if my 
guess is correct.

but theres many instances where it is used such as in function params and in 
constant declares, immediately before, after or with a space, where i dont 
understand there usage, theres even double stars ** here an there...

Would someone be so kind as to write a short tutorial on what the placement of
a 
star means in context to where its used? and how it affects passing by 
reference, by value, or by pointer etc... maybe a few short examples of how it 
would be used.

This would be very appreciated and i would be grateful !
Dec 20 2003
next sibling parent reply Adam Harper <a-news-d harper.nu> writes:
On Sat, 20 Dec 2003 04:19:24 -0500, Lewis wrote:
 One of the most confusing things for me in D or any C style language
 (curly braces and such) is the use of stars * here and there and what
 there placement means...

"*"'s are the pointer declaration/dereference character in C, C++ and D. They are also the multiplication operator. The placement decides whether they are being used as one or the other. Generally, the "*" means multiply when it doesn't: - Follow a type (e.g. "int *", "char *", etc.) - Come before a pointer variable Whitespace doesn't really matter. To give you a code example:
 int i = 1;
 int i2 = 2;
 int* i_ptr = &i;  // "i_ptr" is a pointer to "i"

 i = i * i2; // i = i (multiplied by) i2
 i2 = * i_ptr; // i2 = (The value pointed to by i_ptr)

To declare a pointer you place a "*" before the variable name, any of the following are valid pointer declarations:
 int* i;
 int * i;
 int *i;

To make the pointer actually point to something you need to assign it a reference, this is done by placing the "&" character before a variable name:
 int i = 123;
 int* ptr = &i; // "ptr" points to "i"

To access the value pointed to by a pointer you need to dereference it, this is done by placing a "*" before the pointers name:
 printf( "%d", ptr ); // Will print the memory address that
                      // "ptr" points to, e.g. "-1073743116"
 printf( "%d", *ptr ); // Will print "123"

 for example i know that *MyString would indicate a pointer to MyString if
 my guess is correct.

"*MyString" is a pointer /called/ "MyString", not a pointer /to/ "MyString". Pointers are just variables like everything else and they share the same namespace(?), so you can't have:
 int MyInt;
 int *MyInt;

It's easier if you use the D type declaration [http://www.digitalmars.com/d/declaration.html] (which is also valid in C), where the "*" comes after the type instead of before the variable name:
 int* MyInt;

 but theres many instances where it is used such as in function params
 and in constant declares, immediately before, after or with a space,
 where i dont understand there usage, theres even double stars ** here an
 there...

Pointers are typically used in function params so that a variable is passed by reference rather than value. That way you can modify the variable from within the function and not have to return it so that the caller can use it in it's modified state. Basically, they are used to achieve what D's out/inout function parameters [http://www.digitalmars.com/d/function.html] do. As an example, consider the following function:
 void myFunc( int i )
 {
     i = i+1;
 };

As it stands at the moment it does nothing (at least from the callers perspective). But if we change it to:
 void myFunc( int* i )
 {
     *i = *i+1;
 };

It will increment any integer passed to it (by reference) by one, like so:
 int i = 1;
 myFunc( &i );
 printf( "i = %d\n", i ); // Prints: "i = 2"

Double "*"'s just indicate a pointer to a pointer.
 Would someone be so kind as to write a short tutorial on what the
 placement of a star means in context to where its used? and how it
 affects passing by reference, by value, or by pointer etc... maybe a few
 short examples of how it would be used.

Some good tutorials on pointers in C are: http://pw1.netcom.com/~tjensen/ptr/pointers.htm http://cplus.about.com/library/weekly/aa010502a.htm http://www.lysator.liu.se/c/bwk-tutor.html#pointers http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/pointers.html
 This would be very appreciated and i would be grateful !

As someone else who's been confused by the concept of pointers before, I hope the above is helpful (and correct! If not I'm sure one of the local guru's will correct me).
Dec 20 2003
next sibling parent Lewis <dethbomb hotmail.com> writes:
Very helpful adam, thanks alot... you have answered alot of questions for me.
regards
Lewis


Adam Harper wrote:

 On Sat, 20 Dec 2003 04:19:24 -0500, Lewis wrote:
 
One of the most confusing things for me in D or any C style language
(curly braces and such) is the use of stars * here and there and what
there placement means...

"*"'s are the pointer declaration/dereference character in C, C++ and D. They are also the multiplication operator. The placement decides whether they are being used as one or the other. Generally, the "*" means multiply when it doesn't: - Follow a type (e.g. "int *", "char *", etc.) - Come before a pointer variable Whitespace doesn't really matter. To give you a code example:
int i = 1;
int i2 = 2;
int* i_ptr = &i;  // "i_ptr" is a pointer to "i"

i = i * i2; // i = i (multiplied by) i2
i2 = * i_ptr; // i2 = (The value pointed to by i_ptr)

To declare a pointer you place a "*" before the variable name, any of the following are valid pointer declarations:
int* i;
int * i;
int *i;

To make the pointer actually point to something you need to assign it a reference, this is done by placing the "&" character before a variable name:
int i = 123;
int* ptr = &i; // "ptr" points to "i"

To access the value pointed to by a pointer you need to dereference it, this is done by placing a "*" before the pointers name:
printf( "%d", ptr ); // Will print the memory address that
                     // "ptr" points to, e.g. "-1073743116"
printf( "%d", *ptr ); // Will print "123"

for example i know that *MyString would indicate a pointer to MyString if
my guess is correct.

"*MyString" is a pointer /called/ "MyString", not a pointer /to/ "MyString". Pointers are just variables like everything else and they share the same namespace(?), so you can't have:
int MyInt;
int *MyInt;

It's easier if you use the D type declaration [http://www.digitalmars.com/d/declaration.html] (which is also valid in C), where the "*" comes after the type instead of before the variable name:
int* MyInt;

but theres many instances where it is used such as in function params
and in constant declares, immediately before, after or with a space,
where i dont understand there usage, theres even double stars ** here an
there...

Pointers are typically used in function params so that a variable is passed by reference rather than value. That way you can modify the variable from within the function and not have to return it so that the caller can use it in it's modified state. Basically, they are used to achieve what D's out/inout function parameters [http://www.digitalmars.com/d/function.html] do. As an example, consider the following function:
void myFunc( int i )
{
    i = i+1;
};

As it stands at the moment it does nothing (at least from the callers perspective). But if we change it to:
void myFunc( int* i )
{
    *i = *i+1;
};

It will increment any integer passed to it (by reference) by one, like so:
int i = 1;
myFunc( &i );
printf( "i = %d\n", i ); // Prints: "i = 2"

Double "*"'s just indicate a pointer to a pointer.
Would someone be so kind as to write a short tutorial on what the
placement of a star means in context to where its used? and how it
affects passing by reference, by value, or by pointer etc... maybe a few
short examples of how it would be used.

Some good tutorials on pointers in C are: http://pw1.netcom.com/~tjensen/ptr/pointers.htm http://cplus.about.com/library/weekly/aa010502a.htm http://www.lysator.liu.se/c/bwk-tutor.html#pointers http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/pointers.html
This would be very appreciated and i would be grateful !

As someone else who's been confused by the concept of pointers before, I hope the above is helpful (and correct! If not I'm sure one of the local guru's will correct me).

Dec 20 2003
prev sibling next sibling parent "Mark Brudnak" <malibrud provide.net> writes:
"Adam Harper" <a-news-d harper.nu> wrote

<snip>
 "*"'s are the pointer declaration/dereference character in C, C++ and D.

Just as an aside I like to think of "*" as a dereference operator only. That way in a declatation: int * foo ; can be thought of as 'foo' dereferenced once is an int, so foo must be a pointer to an int. Another example is in a function definition or declaration int oof( float * rab ) ; then rab, when derefernced, is a float, so rab must be a pointer to a float. In this way you can decode complicated declarations like this int * (*bar)() ; Then you can think of 'bar' when dereferenced once is a function, so 'bar' must be a pointer to a function. Again, when the return value is dereferenced it is an int, so it must retrun a pointer to an int. In this way, you can decode any declaration by working from the inside out. Mark.
Dec 20 2003
prev sibling parent reply Lewis <dethbomb hotmail.com> writes:
Adam Harper wrote:
<snip>
 myFunc( &i );


Adam, i noticed you used the Ampersand there, so the &'Variable' is the dereferencing operator? as in myFunc( Address Of i ) or myFunc( Pointer to i ) This has been a most useful thread here...
Dec 20 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Lewis wrote:

 Adam Harper wrote:
 <snip>
 myFunc( &i );


Adam, i noticed you used the Ampersand there, so the &'Variable' is the dereferencing operator?

reference from the variable. Ie you get the owner of the variable. If you want the variable again, you must dereference it using *. int x; int *t = &x; //Now I have the reference of x int g = *x; //Now I have the value of x You might what think about it like looking up a phone number in a book, once someone gives you the name of the person (reference), you can get the person (value) by dereferencing (*). Now say you've got the phone number, and you need to get the reference to the phone number, then you'd use &. In my experience allot of people have problems with understanding pointer initially. They are really very simple once you start to use them, it's all the terms that muddle people up. I've probably just muddled your thinking again, whoopsie. There's lots of info on the web about it. http://cslibrary.stanford.edu/106/
 as in myFunc( Address Of i ) or myFunc( Pointer to i )

 This has been a most useful thread here...

Dec 21 2003
parent Lewis <dethbomb hotmail.com> writes:
J Anderson wrote:
 Lewis wrote:
 
 Adam Harper wrote:
 <snip>
 myFunc( &i );


Adam, i noticed you used the Ampersand there, so the &'Variable' is the dereferencing operator?

reference from the variable. Ie you get the owner of the variable. If you want the variable again, you must dereference it using *. int x; int *t = &x; //Now I have the reference of x int g = *x; //Now I have the value of x You might what think about it like looking up a phone number in a book, once someone gives you the name of the person (reference), you can get the person (value) by dereferencing (*). Now say you've got the phone number, and you need to get the reference to the phone number, then you'd use &. In my experience allot of people have problems with understanding pointer initially. They are really very simple once you start to use them, it's all the terms that muddle people up. I've probably just muddled your thinking again, whoopsie. There's lots of info on the web about it. http://cslibrary.stanford.edu/106/
 as in myFunc( Address Of i ) or myFunc( Pointer to i )

 This has been a most useful thread here...


thanks for the info, very useful... regards lewis
Dec 21 2003
prev sibling parent reply Lewis <dethbomb hotmail.com> writes:
ive notice there is no way to assign a literal to pointer, is there a reason 
this isnt allowed?

//for example id like to do the following:

int Test(int* Value) {
      return *Value + 1;
}

//and then call it like so:

int result;
result = Test(&cast(int)22);
//or
result = Test(&22);

//or another thing i noticed you cant do

int* MyPtr = &34;

//then you could just:
  *MyPtr = *MyPtr + 22;

is there a special reason why a person cant do this? or can it be made so we
can  ;)
Dec 21 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Lewis wrote:

 ive notice there is no way to assign a literal to pointer, is there a 
 reason this isnt allowed?

 //for example id like to do the following:

 int Test(int* Value) {
      return *Value + 1;
 }

 //and then call it like so:

 int result;
 result = Test(&cast(int)22);
 //or
 result = Test(&22);

This code does not make sense as a literal doesn't have a location in memory (per say). Your not archiving anything with the code you just gave. However in C++ you could do: int result = Test(new int(22)); //Not D code But then you'd also have to handle deletion (delete) of the new in created inside the Test function (yuck). If this was made available in D the garbage collection would take care of that. However, int Test(int Value) { return Value + 1; } int result = 22; result = Test(result); Is much better.
 //or another thing i noticed you cant do

 int* MyPtr = &34;

 //then you could just:
  *MyPtr = *MyPtr + 22;

 is there a special reason why a person cant do this? or can it be made 
 so we can 

Again, a literal doesn't have a memory location. Why do you need to change the value 34? How can you access the value 34? In C++ you could do: int* MyPtr = new int(34); //Not D code Still, int t = 34; int* MyPtr = &t; Is almost as good. Furthermore you don't need pointers that often (particularly in D). The code you just wrote could be simplified to: int t = 34; t += 22; Try to avoid pointers where you can. They have all kinds of side effects. Having said that they can be useful in some exceptional circumstances and in arrays.
Dec 22 2003
parent reply Lewis <dethbomb hotmail.com> writes:
J Anderson wrote:

 Lewis wrote:
 
 ive notice there is no way to assign a literal to pointer, is there a 
 reason this isnt allowed?

 //for example id like to do the following:

 int Test(int* Value) {
      return *Value + 1;
 }

 //and then call it like so:

 int result;
 result = Test(&cast(int)22);
 //or
 result = Test(&22);

This code does not make sense as a literal doesn't have a location in memory (per say). Your not archiving anything with the code you just gave. However in C++ you could do: int result = Test(new int(22)); //Not D code But then you'd also have to handle deletion (delete) of the new in created inside the Test function (yuck). If this was made available in D the garbage collection would take care of that. However, int Test(int Value) { return Value + 1; } int result = 22; result = Test(result); Is much better.
 //or another thing i noticed you cant do

 int* MyPtr = &34;

 //then you could just:
  *MyPtr = *MyPtr + 22;

 is there a special reason why a person cant do this? or can it be made 
 so we can 

Again, a literal doesn't have a memory location. Why do you need to change the value 34? How can you access the value 34? In C++ you could do: int* MyPtr = new int(34); //Not D code Still, int t = 34; int* MyPtr = &t; Is almost as good. Furthermore you don't need pointers that often (particularly in D). The code you just wrote could be simplified to: int t = 34; t += 22; Try to avoid pointers where you can. They have all kinds of side effects. Having said that they can be useful in some exceptional circumstances and in arrays.

thanks Mr. Anderson, i was starting to code in D with using almost all pointers for strings, but i have since changed my mind lol... it is alot of complicated stuff im just not ready for yet i dont think, becuase you have to think about each line of code you write, you cant just type it out and know that the variable is the value (instead of a pointer), otherwise your always having to check 'is this a reference or a pointer'?
Dec 23 2003
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Lewis wrote:

 J Anderson wrote:

 Lewis wrote:

 ive notice there is no way to assign a literal to pointer, is there 
 a reason this isnt allowed?

 //for example id like to do the following:

 int Test(int* Value) {
      return *Value + 1;
 }

 //and then call it like so:

 int result;
 result = Test(&cast(int)22);
 //or
 result = Test(&22);

This code does not make sense as a literal doesn't have a location in memory (per say). Your not archiving anything with the code you just gave. However in C++ you could do: int result = Test(new int(22)); //Not D code But then you'd also have to handle deletion (delete) of the new in created inside the Test function (yuck). If this was made available in D the garbage collection would take care of that. However, int Test(int Value) { return Value + 1; } int result = 22; result = Test(result); Is much better.
 //or another thing i noticed you cant do

 int* MyPtr = &34;

 //then you could just:
  *MyPtr = *MyPtr + 22;

 is there a special reason why a person cant do this? or can it be 
 made so we can 

Again, a literal doesn't have a memory location. Why do you need to change the value 34? How can you access the value 34? In C++ you could do: int* MyPtr = new int(34); //Not D code Still, int t = 34; int* MyPtr = &t; Is almost as good. Furthermore you don't need pointers that often (particularly in D). The code you just wrote could be simplified to: int t = 34; t += 22; Try to avoid pointers where you can. They have all kinds of side effects. Having said that they can be useful in some exceptional circumstances and in arrays.

thanks Mr. Anderson, i was starting to code in D with using almost all pointers for strings, but i have since changed my mind lol... it is alot of complicated stuff im just not ready for yet i dont think, becuase you have to think about each line of code you write, you cant just type it out and know that the variable is the value (instead of a pointer), otherwise your always having to check 'is this a reference or a pointer'?

They are numbers used as an index in a huge array. Everytime you want another bit of memory in that huge array, you request it. The program then goes and finds some area in that huge array that is not being used (is free). After a while you don't really need to think about that much (except for the side effects of course), it becomes second nature. One day you may come up against a particular problem and realise, oh that's what pointers are for. Whenever I have to code in a language without pointers (ie ADA, VB) I miss those helpful little stars. -Anderson
Dec 23 2003