Jump to content



Photo
* * * * * 1 votes

Flyingfisch's Prizm C Help Thread


  • Please log in to reply
5 replies to this topic

#1 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 20 March 2013 - 05:15 PM

OK, I am making a thread for me to ask questions on, lets see if I can get this figured out.

I am trying to make a bitmap editor.

here is my code:

#include <color.h>
#include <display.h>
#include <display_syscalls.h>
#include <keyboard.hpp>
#include <keyboard_syscalls.h>
/* USEFUL ROUTINES */
#include "routines.h"


/* FUNCTIONS */
void dispCanvas(int x, int y, int width, int height, int zoom, char canvas) {
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            if (canvas[i][j]) {
                fillArea(i*zoom+x, j*zoom+y, zoom, zoom, canvas[i][j]);
            }
        }
    }
}

/* MAIN FUNCTION */
int main() {
    int key;
    int key_dump;

    int cursor[2] = {1,1};
    int origin[2] = {1,1};

    int canvas_height = 8;
    int canvas_width = 8;
    char canvas[8]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' />;

    canvas[0]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/0.jpg' class='bbc_emoticon' alt='[0]' /> = makeColor(255, 100, 0);
    canvas[0]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' /> = makeColor(255, 100, 0);
    canvas[8]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/0.jpg' class='bbc_emoticon' alt='[0]' /> = makeColor(255, 100, 0);
    canvas[8]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' /> = makeColor(255, 100, 0);

    /* MAIN LOOP, NEVER BREAK */
    while (1) {
        /* GETKEY */
        /*
        switch (PRGM_GetKey()) {
            // handle menu
            case KEY_PRGM_MENU:
                GetKey(&key_dump);
                break;
            // move cursor
            case KEY_PRGM_UP && cursor[0]>0:
                cursor[0]--;
                break;
            case KEY_PRGM_DOWN && cursor[0]<canvas_height:
                cursor[0]++;
                break;
            case KEY_PRGM_LEFT && cursor[1]>0:
                cursor[1]--;
                break;
            case KEY_PRGM_RIGHT && cursor[1]<canvas_width:
                cursor[1]++;
                break;
        }
        */

        dispCanvas(origin[0], origin[1], canvas_width, canvas_height, 4, canvas);
    }


    return 1;
}


And my error.

flyingfisch@Office-Optiplex-745:~/Desktop/PrizmSDK-0.3/projects/paint$ make
/usr/local/cross/bin/sh3eb-elf-gcc -MMD -MP -MF /home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/build/main.d -Os -Wall -mb -m4a-nofpu -mhitachi -nostdlib   -I/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/build -I/home/flyingfisch/Desktop/PrizmSDK-0.3/include -std=c99 -c /home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c -o main.o
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c: In function 'dispCanvas':
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:14:14: error: subscripted value is neither array nor pointer nor vector
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:15:52: error: subscripted value is neither array nor pointer nor vector
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c: In function 'main':
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:63:3: warning: passing argument 6 of 'dispCanvas' makes integer from pointer without a cast [enabled by default]
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:11:6: note: expected 'char' but argument is of type 'char (*)<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' />'
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:26:6: warning: unused variable 'cursor' [-Wunused-variable]
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:24:6: warning: unused variable 'key_dump' [-Wunused-variable]
/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:23:6: warning: unused variable 'key' [-Wunused-variable]
make[1]: *** [main.o] Error 1
make: *** [build] Error 2


Could someone help me figure this out? not sure what a subscripted value is, nor why it can't be int...

(I am very new to C)

#2 Casimo

Casimo

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 641 posts
  • Gender:Not Telling

Posted 20 March 2013 - 07:25 PM

If this is Prizm c (!not c++), you can't write for(int i = ..., because c doesn't support this.
Try to write
int i;
for(i = 0; ...


#3 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 21 March 2013 - 02:45 PM

I am not getting warnings or errors about initializing the var in the for loop, but i changed it anyway.

Now, I got rid of all the errors and warnings except one. I know it is because something is wrong with my pointers, but I don't know what.

#include <color.h>
#include <display.h>
#include <display_syscalls.h>
#include <keyboard.hpp>
#include <keyboard_syscalls.h>
/* USEFUL ROUTINES */
#include "routines.h"


/* FUNCTIONS */
void dispCanvas(int x, int y, int width, int height, int zoom, char **canvas) {
    int i;
    int j;
    for (i = 0; i < width; i++) {
        for (j = 0; j < height; j++) {
            if (canvas[i][j]) {
                fillArea(i*zoom+x, j*zoom+y, zoom, zoom, canvas[i][j]);
            }
        }
    }
}

void dispCursor(int origin_x, int origin_y, int x, int y, int zoom) {
    fillArea(x*zoom+origin_x, y*zoom+origin_y, zoom, zoom, COLOR_RED);
}

/* MAIN FUNCTION */
int main() {
    int key;
    int key_dump;

    int cursor[2] = {1,1};
    int origin[2] = {1,1};

    int canvas_height = 8;
    int canvas_width = 8;
    char canvas[8]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' />;

    int zoom = 4;

    int i;
    int j;

    for (i = 0; i < canvas_height-1; i++) {
        for (j = 0; j < canvas_width-1; j++) {
            canvas[i][j] = COLOR_BLACK;
        }
    }

    canvas[0]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/0.jpg' class='bbc_emoticon' alt='[0]' /> = makeColor(255, 100, 0);
    canvas[0]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/7.jpg' class='bbc_emoticon' alt='[7]' /> = makeColor(255, 100, 0);
    canvas[7]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/0.jpg' class='bbc_emoticon' alt='[0]' /> = makeColor(255, 100, 0);
    canvas[7]<img src='/dot/public/style_emoticons/<#EMO_DIR#>/7.jpg' class='bbc_emoticon' alt='[7]' /> = makeColor(255, 100, 0);

    /* MAIN LOOP, NEVER BREAK */
    while (1) {
        /* GETKEY */
        if (PRGM_GetKey()==KEY_PRGM_MENU) {
            // handle menu
            GetKey(&key_dump);
        }
        // move cursor
        if (PRGM_GetKey()==KEY_PRGM_UP && cursor[0]>0) {
                cursor[0]--;
        }
        if (PRGM_GetKey()==KEY_PRGM_DOWN && cursor[0]<canvas_height) {
                cursor[0]++;
        }
        if (PRGM_GetKey()==KEY_PRGM_LEFT && cursor[1]>0) {
                cursor[1]--;
        }
        if (PRGM_GetKey()==KEY_PRGM_RIGHT && cursor[1]<canvas_width) {
                cursor[1]++;
        }

        // display the canvas
        dispCanvas(origin[0], origin[1], canvas_width, canvas_height, zoom, &canvas);

        // display the cursor
        dispCursor(origin[0], origin[1], cursor[0], cursor[1], zoom);
    }


    return 1;
}

/home/flyingfisch/Desktop/PrizmSDK-0.3/projects/paint/src/main.c:77,71 - Warning - incompatible pointer types passing 'char (*)<img src='/dot/public/style_emoticons/<#EMO_DIR#>/8.jpg' class='bbc_emoticon' alt='[8]' />[8]' to parameter of type 'char **' [Disable with -Wno-incompatible-pointer-types]

When running this addin the calc does a system error.

#4 Casimo

Casimo

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 641 posts
  • Gender:Not Telling

Posted 21 March 2013 - 06:15 PM

Write char *canvas instead ;)
** is a pointer to a pointer (so you probably would need *canvas[i][j], but that doesn't make much sense), you need only a pointer to an array.

EDIT:

I forgot to mention that you should read from the pointer this way then: ...zoom, zoom, *(canvas + i * width + j).


Short explanation:
memory with adresses:
[] [] [] [] []...
0x1,0x2,0x3,0x4,0x5...
(lotsa space, there's nothing saved)

Now you create an array (chars) X3 = {1, 2, 3}. The compiler takes some memory to save the data:
<span class=1' /> <span class=2' /> <span class=3' /> [] []...
0x1,0x2,0x3,0x4,0x5...
(now there is something!)

You want to create a pointer Xptr. You write Xptr = &X, so Xptr is set to the first element of X (= 0x1). (I don't exactly know how a pointer is saved - imagine it would be saved like a normal, single byte -> the compiler takes this byte to save the pointer)

<span class=1' /> <span class=2' /> <span class=3' /> [0x1][]...
0x1,0x2,0x3,0x4,0x5...
(now there is your data and the pointer)

Now you can write X2 or *(Xptr + 2), then you fetch the '3'.

So when you use a pointer to a pointer (we call it Xptrptr), the memory probably looks like this:

<span class=1' /> <span class=2' /> <span class=3' /> [0x1][0x4]...
0x1,0x2,0x3,0x4,0x5...
(data, pointer to data, pointer to pointer of data)

So you have to write *Xptrptr, when you want to have the adress of Xptr, and **Xptr when you want to have the first element of X. Why? Xptrptr is taken -> go to Xptrs memory position -> this is taken as pointer again; it points to data.

I hope everything is correct.

Edited by Casimo, 22 March 2013 - 06:24 PM.


#5 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 21 March 2013 - 08:30 PM

Thanks for the help!

I will post back here when i run into another problem. ;)

#6 Casimo

Casimo

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 641 posts
  • Gender:Not Telling

Posted 22 March 2013 - 06:26 PM

http://www.cemetech....a90efc99#202376
(Without words ;)...)

Accessing to arrays by a pointer is also possible this way.
My way is the way I like it - pointer to pointer scares me.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users