December 23, 2003 Re: twinkle twinkle little stars | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lewis | 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'?
>
In my mind pointers are values too. They are simply indexing values. 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
|
Copyright © 1999-2021 by the D Language Foundation