-
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?
-
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.
-
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
-
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
-
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) );
-
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.
-
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.
-
Code:
uInt8 *image1D;
*image1D = (uInt8*) malloc (320*240*sizeof(Int8));
error C2371: 'image1D' : redefinition; different basic types
what is up with this?
-
Quote:
Originally Posted by
chico1st
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
-
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
-
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.
-
will this return all of image1D?
right now I cannot index through image1D in original program.. there is only one value in image1D
-
You will have to pick one (and only one) of the following:
- 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!
- 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).
-
Or you could return a int (*)[240] for a 2D array.
-
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?