Thread: global array

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    global array

    hey there I want to define an array globally, send it into a function, and use it as a return value.

    I have this
    Code:
    image1D = (uint8*) malloc (320*240*sizeof(int8) );
    
    int main(void){
       imgSnapArea(Sid, (void *)&image1D)
       return image1D;
    }
    does taht make sense?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't make sense to make it global to pass it to another function and then return it from that function. You can just make the variable local.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) You wouldn't return it from main(). When main returns the program ends!
    2) The function which returns has to have the appropriate type. So it would by, instead of main() my_main():
    Code:
    uint8 * my_main() {
    ..
    return imageID
    }
    I suppose imageID is a pointer to uint8
    3) As Elysia pointed out there is no point doing so. When you change a global variable it's value is changed. So why return it's value? Returning is useful in that way only for local variables. Local variables are lost after the function ends so you need to return its value

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    this is a library which is called on by another program.. which also calls on another library.. so i will try this and see what happens

  5. #5
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok i am getting this error from my global variable definition

    Code:
    error C2099: initializer is not a constant
    Code:
    uInt8 image1D = (uInt8*) malloc (320*240*sizeof(Int8) );

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The initializer is, indeed, not a constant. You can declare on one line and malloc on the next, if you must. And why are you trying to assign a pointer into a number?

    And I thought you said you had an array somewhere.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Global variables cannot be initialized with non-constant values.
    Define it, then initialize it in your code.
    Or avoid global vars altogether. They're evil.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Code:
    uInt8 *image1D;
    *image1D = (uInt8*) malloc (320*240*sizeof(Int8));
    
    error C2371: 'image1D' : redefinition; different basic types
    what is up with this?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chico1st View Post
    Code:
    uInt8 *image1D;
    *image1D = (uInt8*) malloc (320*240*sizeof(Int8));
    
    error C2371: 'image1D' : redefinition; different basic types
    what is up with this?
    This is outside a function, right?

    You can not call malloc() [or any other function] outside of a function. You need to put the malloc call into a function somewhere.

    --
    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.

  10. #10
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok ill settle with you... i am trying to do this because when i make function calls to this matlab code every once and a while i get an assertion error.. which means that the arrays are being written outside the limits of what matlab defined

    EDIT: ok so i just tried defining my array like this
    Code:
    uInt8 image1D[320][240];
    but i get

    Code:
    'return' : 'uInt8' differs in levels of indirection from 'uInt8 [320][240]'
    from
    Code:
    uInt8 Foo(void)
    {
    ...
    return image1D;
    }
    I would like to send back the array
    Last edited by chico1st; 06-23-2008 at 02:25 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Again: *image1D is a uInt 8, while the right-hand side is a pointer.

    Also, I said "the line below" before, without thinking; you can only execute code *inside a function* somewhere. I still don't see why you don't do uInt8 image1D[320][240], since that works. But you can't dynamically allocate a global variable.

  12. #12
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    will this return all of image1D?

    right now I cannot index through image1D in original program.. there is only one value in image1D

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You will have to pick one (and only one) of the following:
    1. Global variable. In this case you define image1D outside every function and never ever ever ever ever ever ever return it. It's global -- everyone can see it!
    2. Dynamically allocated variable. In this case, image1D would only appear inside some function somewhere, that would allocate the memory, and perhaps fill it, and then return the pointer to the variable. You would need to declare an uInt8* -- or if you wanted it to be 2D, a uInt8** -- and call an initializer function to set it up.


    Edit: You said this was interfacing with Matlab, and Matlab doesn't understand uInt8**, so you should go with the uInt8* (of course, Matlab would need to know the size, et cetera).

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or you could return a int (*)[240] for a 2D array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    but if another program is calling this library cant i then return the global variable.

    what is if passed the array in by reference and then made the pointer reference the global variable?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob : Creating a global array?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2008, 10:38 AM
  2. Increase size of global multidimensional array
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 04-22-2006, 09:00 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Initilizing global array to all zeroes
    By Durafei in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2003, 12:33 PM