473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing a pointer as argument to a function!!

3
I want to pass a pointer to a function and when the function returns, the pointer must maintain the state! EXAMPLE:

int main(){
byte *myPtr = NULL;
int status;

status = game(myPtr);

cout << myPtr[0] << endl;
}

int game(byte *ptr){
ptr = (byte *) malloc(sizeof(byte) * 10);
ptr[0] = 1;
return 20;
}

The problem is that the pointer when Return from the function game, loose the reference to the allocated space!!

Anyone can help me?

Thanks!!
Dec 2 '06 #1
1 1962
horace1
1,510 Expert 1GB
the function call
Expand|Select|Wrap|Line Numbers
  1.   status = game(myPtr);
  2.  
passes a copy of the value of ptr into function game() so any information stored in it is lost on function return.
what you need to do is to pass the address of ptr so that the address of the storage allocated by malloc() can be assigned to ptr
Expand|Select|Wrap|Line Numbers
  1. // ptr is a pointer to a pointer to byte
  2. int game(byte **ptr){
  3. *ptr = (byte *) malloc(sizeof(byte) * 10);
  4. (*ptr)[0] = 1;     // assign some values
  5. (*ptr)[1] = 2;
  6. return 20;
  7. }
  8.  
  9. int main(){
  10. byte *myPtr = NULL;
  11. int status;
  12. status = game(&myPtr);  // pass address of ptr
  13. cout << (int) myPtr[0] << endl;  // see if values are OK
  14. cout << (int) myPtr[1] << endl;
  15. }
  16.  
  17.  
  18.  
the parameter ptr to function game() is now byte** a pointer to pointer to byte and you have dereference it for malloc() etc
Dec 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
15
by: jacob navia | last post by:
Problem You want to ensure that a pointer argument to a function is non-null. Solution int fn(double data); This means that the array (that is passed as a pointer)
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
2
by: thangviet | last post by:
Hey guys, I have a function which needs to read a file and use the pointer parameter given. the pointer is a structure with five members; int points, float *time, *dhdt, *drate and *diff. The .dat...
2
by: jbd | last post by:
Hi I'm adapting some code I've written using 2d arrays (to represent matrices) to handle large arrays such that double matrix goes to double **matrix and then I'm using malloc. It seems to work...
5
by: dissectcode | last post by:
Hello - Isn't this right? : void Function(int *d, const int c, int n); void Function(int *d, const int c, int n) { while(n) { *(d++) = c; } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.