digitalmars.D.learn - basic question about adresses and values in structs
- nikki (15/15) Sep 01 2014 so I am still very new to structs and & and * adress and pointer
- nikki (1/1) Sep 01 2014 sorry could have quicker just googled it thanks!
- Gary Willoughby (7/22) Sep 01 2014 void func(ref S thing){
- nikki (2/2) Sep 01 2014 ah so much cleaner then the mess I was almost into ;)
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/10) Sep 01 2014 In case they are useful to you or somebody else, the following chapters
- nikki (18/18) Sep 04 2014 thanks! just what I needed, with some stumbling I managed to get
- evilrat (5/23) Sep 04 2014 void** (double ptr) is a pointer to array of pointers(just
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (26/29) Sep 04 2014 If possible, even in C, I would recommend using a 'function pointer' for...
- nikki (3/17) Sep 04 2014 Ah right I was so busy with these * and & ;)
so I am still very new to structs and & and * adress and pointer stuff, I have this basic code : struct S { int value = 0; } void func(S thing){ writeln(&thing); //BFC52B44 thing.value = 100; } S guy = {value:200}; writeln(&guy); //BFC52CCC func(guy); writeln(guy.value);// this prints 200, because the adress was not the same I think I see whats going on but I don't know how to fix it?
Sep 01 2014
On Monday, 1 September 2014 at 18:08:48 UTC, nikki wrote:so I am still very new to structs and & and * adress and pointer stuff, I have this basic code : struct S { int value = 0; } void func(S thing){ writeln(&thing); //BFC52B44 thing.value = 100; } S guy = {value:200}; writeln(&guy); //BFC52CCC func(guy); writeln(guy.value);// this prints 200, because the adress was not the same I think I see whats going on but I don't know how to fix it?void func(ref S thing){ writeln(&thing); thing.value = 100; } The ref keyword passes the variable into the function by reference, so that it is not copied.
Sep 01 2014
ah so much cleaner then the mess I was almost into ;) thanks
Sep 01 2014
On 09/01/2014 11:23 AM, nikki wrote:ah so much cleaner then the mess I was almost into ;) thanksIn case they are useful to you or somebody else, the following chapters are relevant. Value Types and Reference Types: http://ddili.org/ders/d.en/value_vs_reference.html Pointers: http://ddili.org/ders/d.en/pointers.html Ali
Sep 01 2014
thanks! just what I needed, with some stumbling I managed to get everything working as intended: using a pointer variable to save an adres of a function, then dereferencing to use it. Now I am wondering when to use the ** ? for example I found this function over at https://github.com/d-gamedev-team/gfm/blob/master/core/gfm/core/memory.d void* storeRawPointerAndReturnAligned(void* raw, size_t alignment) nothrow { enum size_t pointerSize = size_t.sizeof; char* start = cast(char*)raw + pointerSize; void* aligned = nextAlignedPointer(start, alignment); void** rawLocation = cast(void**)(cast(char*)aligned - pointerSize); *rawLocation = raw; return aligned; } it's a little over my head yet..
Sep 04 2014
On Thursday, 4 September 2014 at 09:54:57 UTC, nikki wrote:thanks! just what I needed, with some stumbling I managed to get everything working as intended: using a pointer variable to save an adres of a function, then dereferencing to use it. Now I am wondering when to use the ** ? for example I found this function over at https://github.com/d-gamedev-team/gfm/blob/master/core/gfm/core/memory.d void* storeRawPointerAndReturnAligned(void* raw, size_t alignment) nothrow { enum size_t pointerSize = size_t.sizeof; char* start = cast(char*)raw + pointerSize; void* aligned = nextAlignedPointer(start, alignment); void** rawLocation = cast(void**)(cast(char*)aligned - pointerSize); *rawLocation = raw; return aligned; } it's a little over my head yet..void** (double ptr) is a pointer to array of pointers(just imagine a crossword where each horizontal letter is part of vertical word) there is little reason to use them in D, mostly to C/C++ interfacing
Sep 04 2014
On 09/04/2014 02:54 AM, nikki wrote:a pointer variable to save an adres of a function, then dereferencingto useit.If possible, even in C, I would recommend using a 'function pointer' for that. However, there are cases where the signature of the function should be unknown to the code that is storing it so a void* is used. (Note that, as discussed on these forums in the past, void* has always been intended to be a data pointer. The fact that it works for function pointers is something we get as lucky accidents, which will most probably always supported by compilers and CPUs.) Here is how D does function pointers: http://dlang.org/expression.html#FunctionLiteral And a chapter that expands on those: http://ddili.org/ders/d.en/lambda.htmlNow I am wondering when to use the ** ?The simple answer is when dealing with the address of a type that is 'void*' itself. In other words, there is nothing special about **: It appears as the type that is "a pointer to a pointer". Inserting spaces: int * p; // A pointer to an int void* * q; // A pointer to a void* // (untested) static assert (is (typeof(*p) == int)); static assert (is (typeof(*q) == void*)); int i; *p = i; // Can store an int void* v; *q = v; // Can store a void* Ali
Sep 04 2014
On Thursday, 4 September 2014 at 14:00:14 UTC, Ali Çehreli wrote:On 09/04/2014 02:54 AM, nikki wrote:Ah right I was so busy with these * and & ;) Thanks!a pointer variable to save an adres of a function, thendereferencing to useit.If possible, even in C, I would recommend using a 'function pointer' for that. However, there are cases where the signature of the function should be unknown to the code that is storing it so a void* is used. (Note that, as discussed on these forums in the past, void* has always been intended to be a data pointer. The fact that it works for function pointers is something we get as lucky accidents, which will most probably always supported by compilers and CPUs.) ... Ali
Sep 04 2014