how to make a function return a pointer

This is a discussion on how to make a function return a pointer within the C Programming forums, part of the General Programming Boards category; look, i know im a noob and i shouldnt be playing around with images yet, but anyway im trying to ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    how to make a function return a pointer

    look, i know im a noob and i shouldnt be playing around with images yet, but anyway im trying to put a piece of code which loads a bitmap into a function which returns the pointer to an array of bytes which represents the bitmap.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    int main(int argc, char *argv[])
    {
        BYTE *lpBits;
        lpBits = loadPicture();
        int i;
        for(i = 0; i < 160; i = i + 4) {
                printf("%d %d %d %d \n", *(lpBits + i), *(lpBits + i + 1), *(lpBits + i + 2), *(lpBits + i + 3));        
        }
        
        
    
        system("PAUSE");	
        return 0;
    }
    
    unsigned int loadPicture()
    {
             HANDLE hBitMap = LoadImage(0, "img.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        
             BITMAP bitmap;
             GetObject(hBitMap,sizeof(BITMAP),&bitmap);
             int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel/8;
            
            printf("%d %d %d %d \n", bitmap.bmHeight, bitmap.bmWidth, bitmap.bmBitsPixel, size);
         
            BYTE *lpBits = malloc(size);
            
            
            GetBitmapBits((HBITMAP)hBitMap,size,lpBits );
            return lpBits;
    }
    i basically dont know what the function should return. A pointer is really just an unsigned int, so i tried that and it didnt work. I couldnt really find any examples lying around either. If ive screwed something else up or theres a better way to do something let me know as well, i am just starting at this.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    //this function prototype returns a pointer of type BYTE
    BYTE* function(int input);

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Quote Originally Posted by *DEAD*
    look, i know im a noob and i shouldnt be playing around with images yet
    Playing around is a great way to learn.

    Quote Originally Posted by *DEAD*
    i basically dont know what the function should return.
    It looks like you're trying to return lpBits:
    Code:
        return lpBits;
    ...and you already know what it is:
    Code:
       BYTE *lpBits = malloc(size);
    ...so, should loadPicture() be returning an unsigned int, or a BYTE pointer?

    Disclaimer: I didn't check any of your code for accuracy. The lack of criticism should in no way be considered an endorsement.
    Jason Deckard

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Also, Wraithan provides much faster answers than I do
    Jason Deckard

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    wholy crap that was fast. Anyway, im still getting errors.

    10 C:\Dev-Cpp\Blobber\mainBack.c [Warning] assignment makes pointer from integer without a cast
    22 C:\Dev-Cpp\Blobber\mainBack.c conflicting types for 'loadPicture'
    10 C:\Dev-Cpp\Blobber\mainBack.c previous implicit declaration of 'loadPicture' was here

    ill go through it from my point of view and maybe that will make my mistake obvious;

    BYTE *lpBits;
    lpBits = loadPicture();

    lpBits is declared as a pointer which points to a BYTE
    the location which lpBits points to is set equal to the location of the array of BYTE which was created in loadPicture()

    as for the implicit declaration of 'loadPicture' was here, im not entire sure what that means.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    10 C:\Dev-Cpp\Blobber\mainBack.c previous implicit declaration of 'loadPicture' was here
    You should have your prototype declared first

    Code:
    BYTE * loadpicture(void)
    and dont use system("PAUSE");

    use getchar which is standard fucnction to just hold on the console window till u hit key.

    ssharish2005

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    haha ive been playing around with too much python. I had the function declared after the call. Thanks fall all the help guys. Everything is working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21