LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2010, 02:16 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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
 
Old 06-10-2010, 02:54 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.
 
1 members found this post helpful.
Old 06-10-2010, 02:59 AM   #3
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
It's because a is a char and byte is an unsigned char. Different interpretation.
 
1 members found this post helpful.
Old 06-10-2010, 03:10 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by Guttorm View Post
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

I modified the code as follows and it worked.
Code:
functionA ((unsigned char*)byte);
I need to revise the pointers now
 
Old 06-10-2010, 03:12 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by rkski View Post
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
 
Old 06-10-2010, 03:30 AM   #6
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
I was too fast to respond. that was not the problems.

You could just make the call even simply:

functionA(byte);
 
1 members found this post helpful.
Old 06-10-2010, 03:34 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by rkski View Post
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 View Post
You could just make the call even simply:

functionA(byte);
Thanks again !
 
Old 06-10-2010, 03:38 AM   #8
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Seg Fault. Memory address not being passed to function. prushik Programming 4 04-21-2010 06:39 AM
Max. Value that could be passed to an udelay() function ananth86coolguy Linux - Newbie 2 04-20-2009 08:44 AM
Parenthesis and other odd characters passed to bash function jakeo25 Programming 2 04-03-2008 10:27 AM
Array structs - passed to function cdog Programming 4 02-02-2006 03:07 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration