Getting started, languages, syntax? October 06, 2008 03:20AM | Registered: 16 years ago Posts: 20 |
Re: Getting started, languages, syntax? October 06, 2008 04:57AM | Registered: 16 years ago Posts: 114 |
Re: Getting started, languages, syntax? October 06, 2008 06:04AM | Registered: 16 years ago Posts: 20 |
Quote
AerialX
rmode is indeed a pointer, and -> has no other use other than accessing fields/members from a pointer (besides the rare case of using operator overloading, anyway).
Quote
Hey, the home button returns to loader, you don't have to reset the whole machine :P
Re: Getting started, languages, syntax? October 06, 2008 12:05PM | Registered: 16 years ago Posts: 443 |
Quote
DigitalManQuote
AerialX
rmode is indeed a pointer, and -> has no other use other than accessing fields/members from a pointer (besides the rare case of using operator overloading, anyway).
Interesting. At one point, I knew about all there was to know about standard (non-Windows-API) C++ programming, and I don't even recall pointers having members or fields. I'm certainly not saying you're wrong, obviously - I just need to find some place with detailed information on this, assuming it's not Wii-specific.
Re: Getting started, languages, syntax? October 07, 2008 02:39AM | Registered: 16 years ago Posts: 138 |
Re: Getting started, languages, syntax? October 07, 2008 03:46AM | Registered: 16 years ago Posts: 20 |
Quote
HyperHacker
Simple comparison of . and -> operators:typedef struct MyStruct { int Foo; }; MyStruct S, *SP = &S; //SP is pointer to S S.Foo = 42; printf("%d\n", SP->Foo); //prints 42
Re: Getting started, languages, syntax? October 07, 2008 09:19AM | Registered: 16 years ago Posts: 443 |
Sub Main() Dim myX As Integer myX = 2 MulX2v(myX) ' myX will still equal 2, because the value is passed MulX2r(myX) ' myX will actually equal 4, because the variable is passed, and not the value End Sub Sub MulX2v(ByVal X As Integer) X = X * 2 End Sub Sub MulX2r(ByRef X As Integer) X = X * 2 End Sub
void Main() { int myX; myX = 2; MulX2v(myX); // myX still equals 2, passed value, not variable MulX2r(&myX); // myX equals 4, passed variable, not value } void MulX2v(int X) { X = X * 2; } void MulX2r(int *X) { X = X * 2; }
Re: Getting started, languages, syntax? October 07, 2008 06:22PM | Admin Registered: 16 years ago Posts: 180 |
// Functions must be declared before they can be used :P void multi_2v(int x) { x = x * 2; // This statement will have no real effect, except using up some cycles and memory. } void multi_2r(int *x) { *x = *x * 2; // for pointers, "var" gives you access to the stored memory address, // and "*var" lets you access the variable stored at that // memory address (interpreted as pointer type) } int main() { int my_x = 2; multi_2v(my_x); // Passed as the value of the variable printf("%d\n", my_x); // Prints 2 multi_2r(&my_x); // Passed as a the memory address of the variable (pointer) printf("%d\n", my_x); // Prints 4 return 0; // return plx kthx }
void multi_2(int &x) { x = x + 2; }Pass by reference works like pass by pointer, but you don't have to dereference your pointer each time you want to use the data at the memory address. Nothing that special about it.
Re: Getting started, languages, syntax? October 07, 2008 09:33PM | Registered: 16 years ago Posts: 443 |
#include <stdio.h> typedef struct myDataStruct { int varX; } myDataStruct; void MulX2v(myDataStruct X); void MulX2r(myDataStruct *X); void main() { myDataStruct myData; myData.varX = 2; MulX2v(myData); // myX still equals 2, passed value, not variable printf("%d\n", myData.varX); // prints 2 MulX2r(&myData); // myX equals 4, passed variable, not value printf("%d\n", myData.varX); // prints 4 } void MulX2v(myDataStruct X) { X.varX = X.varX * 2; } void MulX2r(myDataStruct *X) { X->varX = X->varX * 2; }
Re: Getting started, languages, syntax? October 07, 2008 10:04PM | Registered: 16 years ago Posts: 441 |
Re: Getting started, languages, syntax? October 08, 2008 07:08AM | Admin Registered: 16 years ago Posts: 180 |
Your code there will work just fine. The '->' operator accesses a member of an object at a pointer. It would be similar to something like (*X).varX, except that's just crazy.Quote
whodares
I've adjusted mine to use a data structure
*snip*
It compiles and runs correctly. Am I mixing C and C++? I must admit, neither have really been my strong point.
Re: Getting started, languages, syntax? October 08, 2008 07:22AM | Registered: 16 years ago Posts: 20 |
Re: Getting started, languages, syntax? October 08, 2008 01:40PM | Registered: 16 years ago Posts: 114 |
Re: Getting started, languages, syntax? October 08, 2008 02:04PM | Registered: 16 years ago Posts: 20 |