LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Value gets changed when passed to a function in C++ (https://www.linuxquestions.org/questions/programming-9/value-gets-changed-when-passed-to-a-function-in-c-813284/)

Aquarius_Girl 06-10-2010 02:16 AM

Value gets changed when passed to a function in C++
 
Why do I see different values of character 'a', in main() and in functionA() in the following C++ code?

Code:

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

void functionA (unsigned char *data)
{
    printf ("\ndata in functionA: %d\n", data[0]);
}

int main ()
{
    unsigned char *byte = new unsigned char [1];
    char          a    = 'a';
   
    memcpy (byte, &a, 1);
    printf ("\ndata in main: %d\n", byte[0]);
   
    functionA ((unsigned char*)&byte);
}

The output I am receiving is as follows:
Code:

anisha@linux-uitj:~> g++ qqqqq.cpp -Wall -Wextra
anisha@linux-uitj:~> ./a.out

data in main: 97

data in functionA: 16


Guttorm 06-10-2010 02:54 AM

Hi

You define "byte" like as a pointer to unsigned char. The paramter you send to functionA is "(unsigned char*)&byte" which means the address of the pointer typecasted to an unsigned char pointer. I guess you intend to pass the pointer to the function, not a pointer to the pointer.

rkski 06-10-2010 02:59 AM

It's because a is a char and byte is an unsigned char. Different interpretation.

Aquarius_Girl 06-10-2010 03:10 AM

Quote:

Originally Posted by Guttorm (Post 3998639)
Hi

You define "byte" like as a pointer to unsigned char. The paramter you send to functionA is "(unsigned char*)&byte" which means the address of the pointer typecasted to an unsigned char pointer. I guess you intend to pass the pointer to the function, not a pointer to the pointer.

Thanks for the answer :hattip:

I modified the code as follows and it worked.
Code:

functionA ((unsigned char*)byte);
I need to revise the pointers now :redface:

Aquarius_Girl 06-10-2010 03:12 AM

Quote:

Originally Posted by rkski (Post 3998643)
It's because a is a char and byte is an unsigned char. Different interpretation.

That may be the problem in some cases, but it was not the problem in my case.

Thanks for bothering :)

rkski 06-10-2010 03:30 AM

I was too fast to respond. that was not the problems.

You could just make the call even simply:

functionA(byte);

Aquarius_Girl 06-10-2010 03:34 AM

Quote:

Originally Posted by rkski (Post 3998675)
I was too fast to respond. that was not the problems.

Yes, I noticed you post and then edit and then delete your reply in my other thread.. That's OK :)

Quote:

Originally Posted by rkski (Post 3998675)
You could just make the call even simply:

functionA(byte);

Thanks again !

rkski 06-10-2010 03:38 AM

LOL the other thread I was responding to the wrong information, to another poster and not the values in the original question.
So i just wanted to delete to whole post but i guess i should have made an edit note.


All times are GMT -5. The time now is 02:43 PM.