Thread: Question about array values

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    18

    Question about array values

    So I have an array (several, actually), which holds the RGB values of an image. I'm then doing some calculations with each of these arrays (let's say we're doing Red. Then Red[i][j] is the intensity of "redness" at the pixel (i,j)). Red is of type int, raster is of type uint32. My calculations depend on Red[i][j] being some nice number less than 256. When I run gdb, and tell it to print Red[7], for example, I get something along the lines of
    Code:
     (int*) 0xd3ff20
    .
    Can anyone tell me why it is doing this? Thanks.
    Here is the relevant (hopefully) code
    Code:
    raster = (uint32*) _TIFFmalloc(npixels * sizeof(uint32));
        if (raster == NULL) printf("no memory");
        TIFFReadRGBAImage(tif, w, h, raster, 0); 
        for (i = 0; i<h; i++)
        {
            for (j = 0; j<w; j++)
            {
    
                z = raster[i*w + j]; 
                Red[i][j] = (z & 0xff);
                Grn[i][j] = ((z>>8) & 0xff);
                Blu[i][j] = ((z>>16) & 0xff);
    
            }
        }

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Because red[7] is an array itself, and gets printed as a pointer. You do know pointers and arrays well, don't you?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    18
    Oh. I thought that Red[7] would decompose to Red[0][7]. Sorry.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A speedup:

    Code:
    raster = (uint32*) _TIFFmalloc(npixels * sizeof(uint32));
        if (raster == NULL) printf("no memory");
        TIFFReadRGBAImage(tif, w, h, raster, 0); 
    
        DWORD dwOffset=0;
        DWORD dwMaxOffset=w*h;
        DWORD dwColor=0;
    
        do
        {
           dwColor=raster[dwOffset];
     
           Red[dwOffset]=(dwColor & 0xFF);
           Grn[dwOffset]=((dwColor>>8) & 0xFF);
           Blu[dwOffset]=((dwColor>>16) & 0xFF);
    
           dwOffset++;
        } while (dwOffset<dwMaxOffset);
    One offset. One multiply. Linear array traversal.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    1) Use descriptive variable names, expecially if you have other people read your code.

    2) For values less than 256 you can use unsigned char.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 2-D array question
    By NLH in forum C Programming
    Replies: 4
    Last Post: 04-21-2009, 05:31 PM
  3. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM