Hacking Calling all Wii programmers, new coder needs some help.

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
I'm starting to understand them though:

& makes it not point to the data of the variable, but instead to the memory address where the variable is stored.

the * I'm a little less clear on, but it sounds like the * makes it read where the variable points to, also rather than its data.

Think of it this way:
reference_operator.gif


In this scenario:

int andy = 25;
int fred = andy;
int ted = &andy; // = 1776, the address

dereference_operator.gif

int beth = *ted; // Value pointed at the address stored in ted

"ted" stores the value "1776" which is the address where the value "25" is stored. As such, beth is assigned the value "25".

Pointers in a nutshell. Read this to get more details.
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
Think of it this way:
reference_operator.gif


In this scenario:

int andy = 25;
int fred = andy;
int ted = &andy; // = 1776, the address

dereference_operator.gif

int beth = *ted; // Value pointed at the address stored in ted

"ted" stores the value "1776" which is the address where the value "25" is stored. As such, beth is assigned the value "25".

Pointers in a nutshell. Read this to get more details.
Yeah, you didn't copy-and-paste any of that. Bet you drew those images yourself too. :rolleyes:

Anyway, he's not ready to learn about pointers or references yet.
 
  • Like
Reactions: Foxi4

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
It's part of newlib, but yeah.
No fooling around with sprintf or converting to strings?

Yeah, I bought a flash cart thinking "It's going to be awesome writing NDS homebrew", then tried it out and hated it. I don't like doing extra work. :lol:

In libnds, you have to do this:
Code:
char String[128] = "Sample Text"; //My string, I'll be storing my text here
s8 Number = 4; //My number
 
sprintf(String, "This is my number: %d", Number);
iprintf(String);

OR you could try using itoa(); and then connect strings together using strcat(); which needless to say is a pain in the butt... unless I'm just dumb and haven't found out a better way of doing this. :rofl2:
Yeah, you didn't copy-and-paste any of that. Bet you drew those images yourself too. :rolleyes:

Anyway, he's not ready to learn about pointers or references yet.
I totally copy-pasted the images (from the provided source link, I might add :P) because I can't be arsed, but the explaination is mine. ;)
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
Think of it this way:
reference_operator.gif


In this scenario:

int andy = 25;
int fred = andy;
int ted = &andy; // = 1776, the address

dereference_operator.gif

int beth = *ted; // Value pointed at the address stored in ted

"ted" stores the value "1776" which is the address where the value "25" is stored. As such, beth is assigned the value "25".

Pointers in a nutshell. Read this to get more details.


I read this same thing in the tutorial Joostin linked me to. Yes, I actually did read it. :P
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
I tried to put it in a more humane tongue for you in case the lenghty tutorial was too much for you to wrap your head around. ;)

Nope, I think I got the part for the &, points to the memory address aka location where the variable is stored, and not the data of the variable.

But if you'd like to explain the * to me in more mundane terms, that one I am less clear of.

The tutorial says this:

" * is the dereference operator and can be read as "value pointed by" "
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
In libnds, you have to do this:

Code:
char String[128] = "Sample Text"; //My string, I'll be storing my text here
s8 Number = 4; //My number
 
sprintf(String, "This is my number: %d", Number);
iprintf(String);
If I might make a suggestion for safety in the future, do this:
Code:
#define MAX_ELEMENTS(x) ((sizeof((x))) / (sizeof((x)[0])))
 
int main(int argc, char **argv) {
...
char String[128] = "Sample Text"; //My string, I'll be storing my text here
s8 Number = 4; //My number
 
snprintf(String, MAX_ELEMENTS(String) - 1, "This is my number: %d", Number);
iprintf(String);
...
}
There is no chance of an overflow, and if you change the length or type of String, it will automatically be adapted in sprintf.
 

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
Nope, I think I got the part for the &, points to the memory address aka location where the variable is stored, and not the data of the variable.

But if you'd like to explain the * to me in more mundane terms, that one I am less clear of.

It really can't be put into more mundane terms then what I've written above - that's about as simple as it gets. :P *ted is a pointer to the address "1776" and at that address, you have the value "25". Using this pointer, you will be assigning that value. :)

Imagine that there's a package and you know the street address where you can pick it up - once you get to that address, you pick up the package, but you only ever need the address to get to it. ;)

If I might make a suggestion for safety in the future, do this (...) There is no chance of an overflow, and if you change the length or type of String, it will automatically be adapted in sprintf.
Oh, don't worry, I'm well-aware of the danger of memory overflows, I was just showing you an incredibly barebones example to illustrate how "simple" functionality can be "convoluted" for no apparent reason. :P
 
  • Like
Reactions: RiCK420

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
It really can't be put into more mundane terms then what I've written above - that's about as simple as it gets. :P *ted is a pointer to the address "1776" and at that address, you have the value "25". Using this pointer, you will be assigning that value. :)

Imagine that there's a package and you know the street address where you can pick it up - once you get to that address, you pick up the package, but you only ever need the address to get to it. ;)


So are you saying with a *variable it gets the memory address like the &variable does, but AND the data too?
 

Foxi4

Endless Trash
Global Moderator
Joined
Sep 13, 2009
Messages
30,825
Trophies
3
Location
Gaming Grotto
XP
29,841
Country
Poland
So are you saying with a *variable it gets the memory address like the &variable does, but AND the data too?
You have to re-trace the operations performed by the program to "get it".

&Variable is a reference, it's used because when declaring a variable, we don't know at which location in memory it's going to stored and sometimes we need that information. The reference operator will allow you to trace a value and use its address.

*Variable is the exact opposite, it's a dereference. The dereference operator performs the opposite task - it takes an address, traces it to a given location in memory and uses the value stored there. It's also used in the exact opposite situation - when we know where our data is, but not the actual values stored.
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
Really, you'll learn about pointers much more by using them. Find some programming assignments or something online that uses pointers and try to do those.
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
I had asked previously, but it never got answered, but is there an equivalent for the:

sleep(1000)

for the DevKitPPC for Wii programming?
I want the program to sleep for 1 second aka 1000 milliseconds.

Seems like it should be easy enough, but can't seem to find any reference to it, and it seems that some of the normal C++ code doesn't work with the Wii DevKitPPC compiler.

Do I need to install more libraries?
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
I had asked previously, but it never got answered, but is there an equivalent for the:

sleep(1000)

for the DevKitPPC for Wii programming?
I want the program to sleep for 1 second aka 1000 milliseconds.

Seems like it should be easy enough, but can't seem to find any reference to it, and it seems that some of the normal C++ code doesn't work with the Wii DevKitPPC compiler.

Do I need to install more libraries?
sleep(1000) will pause the current program (or thread) for 1000 seconds. That's a pretty basic GNU C command. If you are getting an error, that means you just didn't add the correct headers.

You need to accept that you have to start from scratch. You don't understand what you are doing.
 

JoostinOnline

Certified Crash Test Dummy
Member
Joined
Apr 2, 2011
Messages
11,005
Trophies
1
Location
The Twilight Zone
Website
www.hacksden.com
XP
4,339
Country
United States
Some learning documents I came across that may be of help to other new-to-C++ programmers:

Learning C++ Pointers for REAL Dummies:

http://alumni.cs.ucr.edu/~pdiloren/C _Pointers/
You need to learn basic syntax and commands before you go to pointers. I don't know why you are refusing to accept that. I think the fact that you just tried (and actually failed because you don't understand headers) to pause a program for 16 minutes and 40 seconds is indication enough. You then decided to use usleep(1000) when sleep(1) was a more sensible option.
 

RiCK420

Contributing Member
OP
Member
Joined
Oct 2, 2013
Messages
172
Trophies
0
XP
159
Country
United States
Anyways I didn't use usleep(1000).

This is my current code (and I know it's not perfect, but it runs, and I'm learning from it):

Code:
#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <ogc/usbstorage.h>
#include <string.h>
#include <unistd.h>

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

int read_USB_storage (int y)
{
    usbstorage_handle usb_device;
    u32 sector_size, num_of_sectors;
    u8 lun = y; // Logical Unit Number (partition)
    
    USBStorage_Initialize();
      USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors);
    
    printf("Logical Unit Number is: %u \n", lun);
    
    printf("Sector size is: %u \n", sector_size);
    
    printf("Number of sectors is: %u \n", num_of_sectors);
    printf("\n");
    
    int total_space = (((sector_size * num_of_sectors) /1024) /1024);
    printf("Total Space (MB): %u \n", total_space);
    printf("\n");
    
    return 0;
}

int my_sleep(int s)
{
    usleep(s * 1000000);
    return 0;
}

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------

    // Initialise the video system
    VIDEO_Init();
    
    // This function initialises the attached controllers
    WPAD_Init();
    
    // Obtain the preferred video mode from the system
    // This will correspond to the settings in the Wii menu
    rmode = VIDEO_GetPreferredMode(NULL);

    // Allocate memory for the display in the uncached region
    xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
    
    // Initialise the console, required for printf
    console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
    
    // Set up the video registers with the chosen mode
    VIDEO_Configure(rmode);
    
    // Tell the video hardware where our display memory is
    VIDEO_SetNextFramebuffer(xfb);
    
    // Make the display visible
    VIDEO_SetBlack(FALSE);

    // Flush the video register changes to the hardware
    VIDEO_Flush();

    // Wait for Video setup to complete
    VIDEO_WaitVSync();
    if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

    // The console understands VT terminal escape codes
    // This positions the cursor on row 2, column 0
    // we can use variables for this with format codes too
    // e.g. printf ("\x1b[%d;%dH", row, column );
    printf("\x1b[2;0H");
    
    printf("RiCK420 has taken over your Wii! \n");
    printf("\n");
    
    my_sleep(3);
    
    printf("...Just Kidding! Please wait patiently! \n");
    printf("\n");
    
    //Read USB Storage
    
    int x = 0;
    while (x < 3) {
        read_USB_storage (x);
        x = x+1;
    }
    
    // Exiting Routine
    
    printf("Process complete, press [HOME] to EXIT or wait 10s! \n");
    printf("\n");
    
    my_sleep(10);
    
    printf("...EXITING! \n");
    exit(0);

    while(1) {

        // Call WPAD_ScanPads each loop, this reads the latest controller states
        WPAD_ScanPads();
        
        // WPAD_ButtonsDown tells us which buttons were pressed in this loop
        // this is a "one shot" state which will not fire again until the button has been released
        u32 pressed = WPAD_ButtonsDown(0);
        
        // We return to the launcher application via exit
        if ( pressed & WPAD_BUTTON_HOME ) { printf("...EXITING! \n"); exit(0);}
        
        // Wait for the next frame
        VIDEO_WaitVSync();
    }

    return 0;
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Veho @ Veho: Firefox users be like "look at what they have to do to mimic a fraction of our power."