Thread: assigning values

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

    assigning values

    Firstly, to those experienced programmer who will be cringing at my question, i apologise.

    I am trying to perform an invert function on an image.

    I have a image_processor.c which has the main function in it, which includes image_lib.h. image_lib.c has the following function in it.

    Code:
    int image_process_invert(int data,int width,int height)
    {
            int new_data[width][height];
            int i,j;
            for(i=0; i<width; i++)
            {
                    for(j=0; j<height; j++)
                    {
                            int red = ((data[i][j]>>16)&0xff);
                            int blue = ((data[i][j]>>8)&0xff);
                            int green = ((data[i][j])&0xff);
                            red = 255 - red;
                            green = 255 - blue;
                            blue = 255 - green;
                            (new_data[i][j]>>16) = red;
                            (new_data[i][j]>>8) = green;
                            (new_data[i][j]) = blue;
                    }
            }
            return new_data[width][height];
    }
    I get an error when trying to assign the values to new_data.

    image_lib.c:42: error: subscripted value is neither array nor pointer

    If you are able to help, please do.

    Thanks,
    Alex

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int new_data[width][height];
    This isn't standard C - you must be relying on a compiler extension for this.

    > int red = ((data[i][j]>>16)&0xff);
    data is an int (according to your parameter), not a 2D array

    Show how you call this function - expecially how you declare data in the caller.

    If you have
    int data[10][20];
    Then this function would be
    int image_process_invert(int data [ ] [20],int width,int height)

    If you have
    int **data;
    Then this function would be
    int image_process_invert(int **data,int width,int height)

    > (new_data[i][j]>>16) = red;
    Assemble the bytes the other way
    new_data[i][j] = (red<<16) | (green<<8) | blue;

    > return new_data[width][height];
    This returns a value outside of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    If you have
    int data[10][20];
    Then this function would be
    int image_process_invert(int data [ ] [20],int width,int height)
    It looks like he wants to return his array, so wouldn't int* be a better return type? Also, if that's what he want he shouldn't be using variable length arrays anyways because then he'd be returning a pointer to a local array.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    3
    The system is a university system which may have an extension on it, we have just been taught to declare an array as:

    int arrayname[x-maxsize][y-maxsize]

    That is how I have defined my data array, and I call the function using:

    image_data[MAXX][MAYY] = image_process_invert(image_data, width, height);

    I havent yet tried your suggestions, but I will when I have time, just hoping that with this information another flaw may be pointed out.

    Thanks,
    Alex

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't return an array. You really can't return a multidimensional array. You could I suppose wrap it in a structure and return that. However, even if you could return it, your assignment would be incorrect, because you'd be trying to assign something to the space past the end of the array.

    You don't need to return anything here though, because any change you do to the array is going to affect it outside of the function anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by quzah
    You really can't return a multidimensional array.
    Quzah.
    I take it you've tried to return a multidimensional array at one point and time while learning c?

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    3
    I have taken the suggestions on board and changed my code as follows (A different function is shown, but same general principle).

    Code:
    int (*image_process_flip_vertical(int data, int width, int height))[]
    {
    	static int new_data[width][height];
    	int i,j;
    	for(i=0; i<width; i++)
    	{
    		for(j=0; j<height; j++)
    		{
    			new_data[i][j] = data[i][(width-1)-j];
    		}
    	}
    	return (int **)new_data;
    }
    Called by:
    Code:
    image_process_flip_vertical(image_data[MAX_X][MAX_Y],width,height);
    I get the following error though:
    image_processor.c:84: warning: passing argument 1 of 'image_process_flip_vertical' makes pointer from integer without a cast

    I can't figure out why.

    Same thing happens if i change the function declaration to:
    Code:
    int (*image_process_flip_vertical(int data[MAX_X][MAX_Y], int width, int height))[]

    Once again, thanks in advance for any help.

    Alex

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's a crazy-ass function declaration. I don't know what it is you're trying to do, but that's not it.
    Code:
    void image_process_flip_vertival( int data[ MAX_X ][ MAX_Y ], int width, int height )
    {
        ...flip in the same array...
        ...don't bother returning, because there's no point...
    }
    If you must use a seperate array, pass it as another argument, and fill it.
    Code:
    void image_process_flip_vertival( int from[ MAX_X ][ MAX_Y ], int to[ MAX_X ][ MAX_Y ], int width, int height )
    {
        ...flip 'from' into 'to'...
        ...don't bother returning, because there's no point...
    }
    Also, a ** is not a two dimensional array. So returning a ** isn't going to cut it. There's a FAQ on the difference between arrays and pointers that you should probably read.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Multiple Values to Array/ Vector
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2009, 01:15 PM
  2. Replies: 1
    Last Post: 05-31-2009, 04:02 PM
  3. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  4. assigning values to multidimensional array
    By jjj93421 in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2004, 04:31 PM
  5. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 08:33 AM