Thread: pointer problem?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    pointer problem?

    Hi i got the following in my main file:

    Code:
    #include <stdio.h>
    #include <ep100lib.h>
    
    int main(int argc, char **argv)
    {
    ep100_lib_init();
    
    FILE * output = stdout;
    FILE * input = stdin;
    
    char *filename=NULL;
    int image_data[EP100_LIB_MAX_X][EP100_LIB_MAX_Y];
    
    filename = argv[argc-1];
    
    int width;
    int height;
    ep100_lib_init();	
    if (argc <= 1)
    	{
    		fprintf(output,"ERROR: Not enough arguments entered");
    		return(0);
    	}
    
    else if (strcmp(argv[1],"-i") == 0)
    	{		
    
    		ep100_lib_open_image(filename);
    
    		width = ep100_lib_get_image_width();
    		height = ep100_lib_get_image_height();
    	
    		ep100_lib_get_data(image_data);
    		ep100_lib_print_data(image_data);
    		
    		invert_image(image_data, width, height);
    
    		ep100_lib_set_data(image_data);
    		
    		sleep(10);
    		
    		ep100_lib_display_image();
    		
    		ep100_lib_close();
    		
    		return(0);
    	}
    Now in another c file i have a function as follows:

    Code:
    #include <ep100lib.h>
    #include <stdio.h>
    
    int **invert_image(int image_data[EP100_LIB_MAX_X][EP100_LIB_MAX_Y], int width, int height)
    {
    
    int image_data_change[width][height];
    int row, column;
    for(row=0; row<height; row++)
    {
    	for(column=0; column<width; column++)
    	{
    		int red = ((image_data[column][row]>>16)&0xff);
     		int green = ((image_data[column][row]>>8)&0xff);
    		int blue = ((image_data[column][row])&0xff);
    
    		int new_red = 255 - red;
    		int new_blue = 255 - blue;
    		int new_green = 255 - green;
    
    		image_data_change[column][row] = ((new_red<<16) | (new_green<<8) | (new_blue));
    		
    	}
    }
    		return (int **)image_data_change;
    
    }
    Now it doesnt quiet work, not sure why.

    Could someone please advise?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You shouldn't need to cast your return type to a int **. You are doing something wrong if you are casting it.

    I also don't think you should change the array size in your code - it needs to at least have the same stride [I presume, since there is no stride parameter to the "setdata" function].

    You don't seem to have a header file for your image_invert (or a prototype for it).

    And you never pick up the return value from image_invert().

    You are also returning a block of memory that is automatically placed on the stack within your function, which if it's 4MB will possibly overflow the stack, and certainly won't remain intact when you leave the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    in your function:
    Code:
    int red = ((image_data[column][row]>>16)&0xff);
    I've tried this, it didn't work.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    You shouldn't need to cast your return type to a int **. You are doing something wrong if you are casting it.
    ok understood

    I also don't think you should change the array size in your code - it needs to at least have the same stride [I presume, since there is no stride parameter to the "setdata" function].
    what do you mean? i dont see that i am changing the size?


    You don't seem to have a header file for your image_invert (or a prototype for it).
    what do you mean header file?
    like ep100_lib.h

    And you never pick up the return value from image_invert().

    You are also returning a block of memory that is automatically placed on the stack within your function, which if it's 4MB will possibly overflow the stack, and certainly won't remain intact when you leave the function.
    so should i remove the who return statement? will it send the new array back to main and display?

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Quote Originally Posted by Scarecrowm View Post
    in your function:
    Code:
    int red = ((image_data[column][row]>>16)&0xff);
    I've tried this, it didn't work.
    what did you do then?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    ok understood


    what do you mean? i dont see that i am changing the size?
    So width and height are always the same as EP100_LIB_MAX_X & EP100_LIB_MAX_Y?? If so, why do you need variables and a function to get width and height?

    I don't beleive for a second that this is the case, but you are creating a local 2D array that is dimensioned by width and height rather than the EP100_LIB_MAX_X/Y constants. This means that the second row of pixels start at "width" pixels down, rather than EP100_LIB_MAX_X pixels down that at least I am convinced it should.

    Also, using variables to size an array is an extension that is only supported by some compilers - you should really use constants.



    what do you mean header file?
    like ep100_lib.h
    Yes, but one that contains YOUR function - I presume that ep100_lib.h doesn't have a "invert_image()" in it...



    so should i remove the who return statement? will it send the new array back to main and display?
    No, you should USE what's been returned, and make sure that what you return is not a a local variable that "disappears" after you've left the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    Quote Originally Posted by matsp View Post
    So width and height are always the same as EP100_LIB_MAX_X & EP100_LIB_MAX_Y?? If so, why do you need variables and a function to get width and height?
    ok ill change that

    I don't beleive for a second that this is the case, but you are creating a local 2D array that is dimensioned by width and height rather than the EP100_LIB_MAX_X/Y constants. This means that the second row of pixels start at "width" pixels down, rather than EP100_LIB_MAX_X pixels down that at least I am convinced it should.

    Also, using variables to size an array is an extension that is only supported by some compilers - you should really use constants.


    Yes, but one that contains YOUR function - I presume that ep100_lib.h doesn't have a "invert_image()" in it...
    I am using a make file which compiles them both.

    No, you should USE what's been returned, and make sure that what you return is not a a local variable that "disappears" after you've left the function.
    thast where i am a little confused on how i can use the pointer between two files?

    Thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using makefiles have nothing to do with how you tell one part of your source how the other parts functions are declared.

    Prototypes is the way you tell one part of your source that "there is a function called xxx, which takes parameters of this type, and in this order, and it returns this type...".

    When you are dealing with multiple source files, it's generally accepted standard to have a header file for each .c file, which contains all the functions that are "for external use" [so not necessarilty ALL functions, but those functions that you expect others to use from your file - the ones you don't "export" this way, should be declared static so as not to:
    1. name-clash with other functions.
    2. cause someone to think the function should be used outside your function.

    Say for example you invert your colours by calling a invert_pixel() function. This function is no use to someone using the invert_image() function - but it helps you write the code more readable and easier to understand, perhaps. This function should be declared static.

    You should then use the header-file declaring your functions as an include in:
    1. your file defining the functions. This prevents accidental mismatches when you change only the function in the defining file, not in the declaration.
    2. in all files that CALL your function.

    If you enable warnings (always a good thing), the compiler will tell you if you are using a function without a prototype. -Wall is the right thing for gcc - other compilers use other ways to say "enable warnings".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    hmm ok so like in the main i put:
    #include <invert_image.c>?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    hmm ok so like in the main i put:
    #include <invert_image.h>?
    Perhaps as the red marked.

    It is not THE SAME FILE as where you implement the code - it should be liek the ep100_lib.h, contain some constants and prototypes for your functions, but not the actual functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    little confused. Basically i have:
    Image.c - contains main
    Image_lib.c - contains three functions to edit pictures
    ep100lib.h - functions to get picture etc
    makefile - to compile it all

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you probably want a Image_lib.h to be included by image.c and image_lib.c

    That should contain at least the function header for image_invert(), but probably also the other two functions - if the functions are supposed to be used outside of the image_lib.c file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    but i dont have a image_lib.h file.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by taurus View Post
    but i dont have a image_lib.h file.
    No, you need to create one, that's my point.

    It's mainly a bit of copy and paste from your .c file - take the header of the function [up to, but not including, the first {], then put a semicolon to make it a function prototype.

    Put an include guard [search for include guard] in the file too, so you can include it whenever and wherever without causing problems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    the problem is when submitting the files we can only submit 2 miles which are Image.c and Image_lib.c.

    so it would be missing the file and not work. Thats y i think the makefile covers that:
    Code:
    FILE=Image
    CFLAGS=-I. -I/usr/units/ep100/ass2.072/ `imlib2-config --cflags` -g
    $(FILE): $(FILE).o Image_lib.o
    	cc  $(FILE).o Image_lib.o -o $(FILE) `imlib2-config --libs` -L/usr/units/ep100/ass2.072/ -lep100 -g
    
    IMAGE=images/pa300570_small.jpg
    PARAM=
    run: $(FILE)
    	$(FILE) $(PARAM) $(IMAGE)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM