C++ Learning Community Forum
August 01, 2010, 03:22:31 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Help to understand pointers better  (Read 549 times)
bsaikrishna
Geekoid
*
Posts: 178


View Profile
« on: January 06, 2010, 04:25:35 PM »

Code:
#include <iostream.h>

int main () {

int num = 1;
if(*(char *)&num == 1)
{
  printf("\nLittle-Endian\n");
}
else
{
  printf("Big-Endian\n");
}

[font=Verdana]//This prints garbage, not sure why?[/font]
cout<<"num: "<<*(char *)&num<<endl;

//This prints 1
cout<<"num: "<<*(int *)&num<<endl;

}

I am not sure why the first statement prints garbage, but the same works fine in the if block above, can somebody clarify please?
Logged
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« Reply #1 on: January 06, 2010, 04:41:46 PM »

HA, this is funny. I compiled it and when you typecast the int as a char, it prints whatever corrisponding ASCII value the int is. So in this case a smily face.
But whats the point of this exersize? You're not really useing pointers.
Logged
bsaikrishna
Geekoid
*
Posts: 178


View Profile
« Reply #2 on: January 06, 2010, 04:58:39 PM »

Okay, got it.

Also, could you tell me this please? I am just trying to play around understand this casting...

I apologize for the stupid code...

Code:
#include <iostream.h>

int main() {

int a=30;
char b='A';

cout<<"(int*)a : "<<(int*)a<<endl; // why is this not causing segmentation fault
//cout<<"(char*)b : "<<(char*)b<<endl; // why is this causing segmentation fault
cout<<"&a : "<<&a<<endl;
cout<<"(int*)&a : "<<(int*)&a<<endl;
cout<<"(char*)&a : "<<(char*)&a<<endl;
cout<<"*(char*)&a : "<<*(char*)&a<<endl;
cout<<"*(int *)&b : "<<*(int *)&b<<endl;
cout<<"&b : "<<&b<<endl;

return 0;
}

OUTPUT:
------------------------------------------------
(int*)a : 0x1e  -- Why am I getting hexadecimal output, what exactly does this casting mean?
&a : 0x22ff70  -- will the address be displayed in hexadecimal format ?
(int*)&a : 0x22ff70
(char*)&a : ▲  -- as this is an address, why is it not displayed in hexadecimal ?
*(char*)&a : ▲
*(int *)&b : 7745 -- frankly, i donno where this value has come from, please can u explain me?
&b : A▲  -- why not in hexadecimal
« Last Edit: January 06, 2010, 05:03:55 PM by bsaikrishna » Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #3 on: January 12, 2010, 03:15:14 AM »

the & symbol is the address of operator, when you use & before a variable you will get the address of that variable in memory
So what you are getting is the address of a in hex format.
Logged


Imagine the impossible, then make it happen.
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #4 on: January 12, 2010, 06:40:58 AM »

When you pass a pointer to std::cout it prints the value of the pointer (the address where it points).
With one exception. If you pass a char* to std::cout it assumes it is a C-String.
So if you pass a char* then it will print a string of characters upto the firdst character that is '\0' (NULL terminator).


(int*)a : 0x1e  -- Why am I getting hexadecimal output, what exactly does this casting mean?
The above line converts 'a' into a pointer (without changing the value) then prints the pointer.
0x1e:   This is the Hex value of 30.  The 0x indicates the following are hex characters. 1e => (1 * 16) + 14 => 30


&a : 0x22ff70  -- will the address be displayed in hexadecimal format ?
The above line takes the address of 'a' (which is an int*) and print that:


(int*)&a : 0x22ff70
The above line takes the address of 'a' (which is an int*) casts that to an int* (no-op) and prints the hex value.

(char*)&a : ▲  -- as this is an address, why is it not displayed in hexadecimal ?
*(char*)&a : ▲
The above line takes the address of 'a' (which is an int*) casts this to a char* (nothing happens but a type change). You then de-reference the addrss and treat its content like a char (de-referencing a char* gives you a char). The output of this depends on how the hardware represents an integer. Because an integer is probably 4 bytes and the char is only 1 byte you are printing 1/4 of the integr (which part I am not sure without more info).
 
*(int *)&b : 7745 -- frankly, i donno where this value has come from, please can u explain me?
The above line is taking the address of 'b' (which is a char*) then casting this to an int* then de-referencing the pointer to get an integer (the result of this is purely random and depends on not only how the hardware represents integers but also what other values are stored in the locations after the variable b.


&b : A▲  -- why not in hexadecimal
The above is taking the address of 'b' (which is a char*) then printing it. Note that char* is a special case when printing and it is treated like a C-String. We know that 'b' is a char with the value 'A' so that is the first value printed. The value in memory write after 'b' is some random value that when printed generates a triangle on your system. It looks like the 2nd byte after the variable 'b' has the value '\0' and thus stops the printing (C-Strings are NULL ('\0') character sequences.

[/quote]
Logged
bsaikrishna
Geekoid
*
Posts: 178


View Profile
« Reply #5 on: January 13, 2010, 03:04:04 PM »


Thank you very much myork and cpplc.

I will digest the things and I will ask you a few questions if I have doubts.

Thank you for your reply.  Smiley
Logged
mailsubhra
Jr. Nerd
***
Posts: 64


View Profile
« Reply #6 on: February 25, 2010, 12:16:07 PM »

Why is the following code returning segmentation fault?
Code:
cout<<"(char*)b : "<<(char*)b<<endl;

Thanks.
Logged
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #7 on: February 25, 2010, 08:44:46 PM »

What's 'b'?
Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
mailsubhra
Jr. Nerd
***
Posts: 64


View Profile
« Reply #8 on: February 26, 2010, 04:56:10 AM »

Code:
char b='A';
We are trying to typecast a char to a char*
But if
Code:
int a=30;
cout<<"(int*)a : "<<(int*)a<<endl;
is not returning any error why is the char doing so? Is it because the value stored in char b is of non int type and hence cannot be typecasted to a pointer type?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!