3

Im new to C. I have a const unsigned short array of 1024 hex numbers. each hex number represents is 8 bits and represents bits to be turned on and off when displaying an image to a GBA screen. But nevermind all that and the DMA syntax I have below just for reference!!

My main question is...how can I iterate through elements in an array BY ADDRESS, grab those contents, then continue incrementing through addresses? Also, if you could give a stare to the below code and maybe see why Im getting:

"Program.c:(.text+0xe8): undefined reference to `myimg'" 

on the line that calls "drawImage3" and that would be rad.

(in the main of program.C):

const unsigned short *pt;  
pt = &myimg[0]; 
int size = 5;
drawImage3(15,15,img_WIDTH,img_HEIGHT, pt);

(defined elsewhere):

void drawImage3(int x, int y, int width, int height, const u16* image)
{
    int r;
    for (r=0; r<height; r++)
    {   
        DMA[3].src = &image;
        DMA[3].dst = &videoBuffer[OFFSET(x+width, y, 240)];
        DMA[3].cnt = width | DMA_SOURCE_FIXED | DMA_ON |   DMA_DESTINATION_INCREMENT;  
        image++;    
    }
}

2 Answers 2

1

You're setting DMA[3].src to the address of a pointer, which is probably not what you want to do. For clarity's sake, here's what these references mean:

*image    -- the value of the thing which image points to
 image[0] -- same as *image
 image    -- the location in memory of your thing
&image    -- the location in memory that is storing your pointer
&image[0] -- same as image
&image[n] -- the location of the nth element in your thing

So instead of DMA[3].src = &image;, you probably want one of these two:

DMA[3].src = &image[r];    # If you do this do NOT increment image

or

DMA[3].src = image;        # And continue to increment image

If you choose the latter, then

DMA[3].src = image;
image++;

Can be better written as:

DMA[3].src = image++;
Sign up to request clarification or add additional context in comments.

Comments

0

From the code provided, myimg is never defined (line two in the second code block).

As for looping by address, arrays are already pointers, so doing a simple for loop is the same as looping over addresses. I'm not sure what you're trying to accomplish with the 'looping by address' business though, because that's what C is already doing.

EDIT:

AFAIK, arrays don't really exist in C, but they do in C++, so indexing into an 'array' is just saying, 'give me a chunk of memory starting at this address and this many bytes * size into it'.

For example, an int array (4 bytes per index) is just a chunk of memory that is 4 bytes * number of indexes. Getting an index into this 'array' is just getting a memory offset x bytes * 4 (sizeof int) into the memory chunk.

Simply put, you shouldn't have to worry about it much.

9 Comments

I added a little info about what I understand arrays to be.
Not sure what all this new-fangled terminology is, but arrays in c are pretty much pointers.
@Perception that is a very misleading and inaccurate thing to say. @tjameson arrays are basically the same in C and in C++; the only meaningful difference is that in C++ you can create a reference to an array. Everyone should read c-faq.com/aryptr/index.html thoroughly.
@Jeff Mercado - I beg to disagree. Arrays (const pointers) and pointers are much more similar in C/C++ then arrays in other languages that support real properties like for example, the length attribute.
I think arrays are a little more array-like in c++ because you can use the new operator to declare a 'new' array. Since new doesn't exist in c, and since 'new' is just malloc or something similar under the hood, there really isn't a big difference. I just think C++ makes a clearer distinction between a memory block and an 'array'. Sure, arrays in C++ aren't an object, just a memory chunk, but it's a little clearer distinction in my mind.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.