Sending a pointer to a function

Hello,
I am correctly working a project that sends an array to a function but the fuction receives it as a pointer. Below is an example of code similar to what is in the project.

1
2
3
4
5
6
7
8
9
10
int main()
{
unsigned char myStr[] = "test";
read(&(myStr[4]));
}

void read(void* pBuffer)
{
 cout << pBuffer<<endl;
}


My question is why when I print the value of the pointer it reads "0012FF80" instead of "test". Any help will be greatly appreciation.
hi,

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
unsigned char myStr[] = "test";
read(&(myStr[4])); //you requested to to read address of a single char
}

//***********
void read(char pBuffer)  //use char argument 
{
 cout << pBuffer<<endl;
}
read(myStr[4]); //will print a single char
}


or

1
2
3
4
5
void read(char* pBuffer)
{
 cout << pBuffer<<endl;
}
read(myStr) //print whole string 

Last edited on
Codekiddy,
Thanks. I forgot to add a function call to the code above.Below is an example of code similar to what is in the project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
void ParseMsg(unsigned char* pBuffer);
void ProcessMsg(void* pBuffer);

int main()
{
unsigned char myStr[] = "test";
ProcessMsg(&(myStr[4]));
}

void ProcessMsg(void* pBuffer)
{
  ParseMsg((unsigned char*) pBuffer);
}
void ParseMsg(unsigned char* pBuffer);
{
  cout<<p<<endl;
// this is not in the project code but I want to 
//read the data at this point, I am not getting 
//anything. Do you know the reason that I am not getting "test" at thtis point.
}


Any help will be greatly appreciation.
Last edited on
Topic archived. No new replies allowed.