|
What I think you need to understand after having read your previous posts, is that when you call a function the arguments passed are copies on the stack.
Have a look here[^] if you're not sure what the stack is and how it is used.
Let's examine this with some code examples.
A function called AddOne is used for increasing a variable by the value of 1.
void AddOne( int nTheVariable )
{
nTheVariable = nTheVariable + 1;
}
void main()
{
int nMyValue = 0;
printf( "The value is %d.\n", nMyValue );
AddOne( nMyValue );
printf( "The value is %d.\n", nMyValue );
} When AddOne() is declared as above, the value of nMyValue is copied to the stack and read by AddOne() . When the function returns is has indeed increased its local copy (nTheVariable ) by one, but nMyValue still remains the same since it was a copy of it that was passed to the function.
If you want to alter the value of nMyValue , you have to pass its location as argument to AddOne() . "Location" in this aspect means "the address of".
In code it would look like this:
void AddOne( int* pnTheVariable )
{
*pnTheVariable = *pnTheVariable + 1;
}
void main()
{
int nMyValue = 0;
printf( "The value is %d.\n", nMyValue );
AddOne( &nMyValue );
printf( "The value is %d.\n", nMyValue );
}
In your case you want to allocate memory and you always assign it to a pointer.
To be able to alter the address assigned to a pointer, you have to pass a copy of the address of the pointer to the function. This is still the same as above, but now you want to alter the value of a pointer.
In code it would look something like this if you want to allocate memory for three int s:
void Allocate( int** ppnTheMemory )
{
*ppTheMemory = (int*)malloc( 3 * sizeof( int ) );
}
void main()
{
int* pnMyValues = NULL;
Allocate( &pnMyValue );
if( pnMyValues )
{
free( pnMyValues );
pnMyValues = NULL;
}
}
The above means that your original code where you allocate memory for the structure inside the fill() function should look something like this:
void fill( struct mystruct** ppTheStruct );
void main()
{
struct mystruct* pMyStruct = NULL;
fill( &pMyStruct );
...
if( pMyStruct )
{
free( pMyStruct );
pMyStruct = NULL;
}
}
void fill( struct mystruct** ppMyStruct )
{
*ppMyStruct = (struct mystruct*)malloc( sizeof( struct mystruct ) );
....
}
Hope this makes things a little more clear for you.
Good luck.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
I tried this it works ,
but i cannot fix the allocated emory size by a no-static way
void fill(void *input);
struct mystruct {
char *ch;
int n;
};
#define ARG_SIZE sizeof(struct mystruct)
int main(){
void *ptr;
ptr = (void*) malloc (ARG_SIZE);
fill(ptr);
printf ("%i",((struct mystruct*)ptr)->n);
printf ("--%s",((struct mystruct*)ptr) ->ch);
return 0;
}
void fill(void *input){
struct mystruct* st;
st = (struct mystruct*) input;
st ->n = 3;//operation is ok
st->ch = (char *) malloc (3*sizeof(char));
strcpy(st->ch,"OK");
input = (void*) st;
}
|
|
|
|
|
void fill(void *input);
struct mystruct {
char *ch;
int n;
};
#define ARG_SIZE sizeof(struct mystruct)
int main(){
void *ptr;
ptr = (void*) malloc (ARG_SIZE);
fill(ptr);
printf ("%i",((struct mystruct*)ptr)->n);
printf ("--%s",((struct mystruct*)ptr) ->ch);
return 0;
}
void fill(void *input){
struct mystruct* st;
st = (struct mystruct*) input;
st ->n = 3;//operation is ok
st->ch = (char *) malloc (3*sizeof(char));
strcpy(st->ch,"OK");
input = (void*) st;
|
|
|
|
|
i am having an exe in my drive i have to call it from my mfc code. how to do this
|
|
|
|
|
Use CreateProcess or ShellExecute .
|
|
|
|
|
|
I dont know why you get 2 votes(1.8) I give you 5;)
|
|
|
|
|
WhiteSky wrote: I dont know why you get 2 votes(1.8) I give you 5;)
slippery hands
|
|
|
|
|
You are optimistic
|
|
|
|
|
WhiteSky wrote: You are optimistic
you have tooo he he he
|
|
|
|
|
I think optimistic is better than pessimistic it sends affirmative signal to man,right;)
|
|
|
|
|
WhiteSky wrote: I think optimistic is better than pessimistic it sends affirmative signal to man,right;)
as always you are right dude!
|
|
|
|
|
|
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hi
I am writting a application in 64 bit OS from which i want to lunch a 32 bit exe. but i do not know how to lunch a 32 bit exe in 64 bit process with commandline argument. I just tried using CreateProcess api but it is
crashing at the time of lunching.So can any body help me
regarding this.its urgent.
Thanks
tutu
|
|
|
|
|
aaaan wrote: crashing at the time of lunching
what does it say ????
|
|
|
|
|
OS crash happening with blue screen.
tutu
|
|
|
|
|
aaaan wrote: I just tried using CreateProcess api but it is crashing...
What does your code look like?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I want to disable modem and Ethernet using VC++, can anyone help me out.
some code would be appreciable
|
|
|
|
|
i want to make my combo box read only ie when i type anything inside combo box after running my application it shouldn't get typed in it.......
How to do it????????
can anybody plz solve my problem..............
thanks in advance
|
|
|
|
|
Set combo box property to DropList (CBS_DROPDOWNLIST ).
|
|
|
|
|
sulabh_expert@rediffmail.com wrote: ant to make my combo box read only ie when i type anything inside combo box after running my application it shouldn't get typed in it.......
make combo to combolist
|
|
|
|
|
Hi,
my exe is single when i distribute to customer, So i don't want to use dll. How do i convert my dll into lib.
Regards,
Rameshkanth
|
|
|
|
|
ksrameshkanth wrote: [urgent]
The urgent forum is yet to open. If it is really urgent, you can visit http://www.rentacoder.com
|
|
|
|
|
brahmma wrote: If it is really urgent, you can visit http://www.rentacoder.com
he he he!
|
|
|
|