LinuxQuestions.org
Help answer threads with 0 replies.
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 11-24-2006, 06:34 AM   #1
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
C pointers : What will be the output?


Hey,
I guess the topic is self explanatory

Quote:
int main()
{
int i;
float *p;
p=(float*)&i;
*p=100;
printf("%d",i);
}
I said Output will be some number other than 100 whereas answer is something else. Cos *p will try to store floating point (IEEE 32 bit) representation of 100 at address of i. As (sizeof(int))=4byte that is again 32 bits. Thus output will be something not 100. Namely it will be int value of 32 bit IEEE representation of 100. Am I right or wrong? Whats correct?
Quote:
int main()
{
int i;
printf("%d",&i);
scanf("%d",i);
}
This one I have no idea. The compiler seems to behave randomly.

Note I knw the outputs I have compiled in GCC and executed. Tell me what they mean and why is that the output?
 
Old 11-24-2006, 06:47 AM   #2
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Code:
printf("%d",&i);
This prints a memory location (where the value of 'i' will be stored) and this can be pretty much random with every run.
 
Old 11-24-2006, 07:12 AM   #3
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Original Poster
Rep: Reputation: 46
That one I knew. I am wondering abt the scanf thing . That could lead to anything. Sometimes runtime errors sometimes not. Why? If possible could anyone tell me what kind of int values are good as memory addresss?
 
Old 11-24-2006, 11:50 AM   #4
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Code:
scanf("%d",i);
That will try to store the input value to the memory address "<i's value>". So it depends on what value "i" is initialized with.
 
Old 11-24-2006, 12:32 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Code:
// Wrong
int main()
{
  int i;
  printf("%d",&i);
  scanf("%d",i);
}
Code:
#include <stdio.h>
int 
main(int argc, char *argv[])
{
  // Better
  int i;

  printf ("Enter some number:");
  scanf("%d", &i);
  printf("You entered: %d; the address of i is 0x%x", i, &i);

  return 0;
}
 
Old 11-24-2006, 10:53 PM   #6
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Original Poster
Rep: Reputation: 46
Dude, I also knw thats wrong. I am asking what will be the output. My GOD!!! you thought you had to teach how to use scanf and printf???

Anyways , could anyone tell me what values of integer i would be okay if they are used as memory addresses thru

scanf("%d",i);
 
Old 11-24-2006, 11:04 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Dude, I also knw thats wrong. I am asking what will be the output. My GOD!!! you thought you had to teach how to use scanf and printf???
unnecessary, he was trying to help you like the others were.

Quote:
Anyways , could anyone tell me what values of integer i would be okay if they are used as memory addresses thru
you want to set the address of the variable to the input? the only good value would be '0' or the address of another variable. you dont want to set it to anything else.
 
Old 11-24-2006, 11:30 PM   #8
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Original Poster
Rep: Reputation: 46
No I am not setting it. I am generally asking for the fun of it.
Right now the second program in 1st post compiles and works.
Sometimes gives a runtime error though. I guess thats bcos C gives a garbage value to newly declared int unlike Java.

What I wanted to know was what range of ints wouldn't give a runtime error?
 
Old 11-25-2006, 12:42 AM   #9
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
You weren't very specific the first time. Now I got it. You're basically asking what memory addresses would be available to store an int when you call that program. Unfortunately I can't answer that question [don't even know if you can correctly predict those address values].
 
Old 11-26-2006, 03:26 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The first program consistenly outputs 1120403456, which is as you guessed the IEEE 754 single precision representation of the number 100.

Code:
  1120403456
= 0x42c80000
= 01000010110010000000000000000000
= [0] [10000101] [10010000000000000000000]
= [+] [     133] [                 0.5625]

-> + (1+0.5625)^(133-127)
= 1.5625 ^ 6
= 100.
 
Old 11-27-2006, 10:40 AM   #11
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
int main()
{
int i;
float *p;
p=(float*)&i;
*p=100;
printf("%d",i);
}
100 gets added to the address of the pointer p not to the address of i. This is done cause of the derefrence. Thats what I can make of it.
 
Old 11-27-2006, 01:00 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by exvor
100 gets added to the address of the pointer p
No, it is not added and neither to an address.
p is a pointer to a float, and this float is set to 100.
Quote:
not to the address of i.
p points to the same storage space as i.
Quote:
This is done cause of the derefrence. Thats what I can make of it.
I do not understand both of these last statements
 
Old 11-27-2006, 01:42 PM   #13
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
I was looking at *p=100; but i was mistaken so nevermind
 
Old 11-27-2006, 05:34 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,640
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
The example code orders the computer to set 'p' to the address of 'i' and to treat that pointer as "a pointer to a float value," which it obligingly does.

It then orders to store the floating-point number "100.0" into the location pointed-to by 'p', which again it obligingly does. Notice that if 'i' is too small to contain a float, it won't matter: the computer will still store the value in memory as ordered, thus destroying whatever's in those "extra" locations. (A most serious bug.)

The final statement orders the computer to display the value of 'i', which will now contain the binary value equivalent to [the leading bytes of] the floating-point number '100.0.'

A far more desirable method to do such a thing is with the union directive, which stipulates that all of the variables in the union begin at the same address. The size of the union is the size of the largest member. Thus:
Code:
  union {
    int   i;
    float f;
  }
  f := 100.0;
  printf("%d\n", i);
will do the same thing, but safely. This is because both 'i' and 'f' occupy the same area of memory, and the space reserved for the union is sizeof(i) or sizeof(f), whichever is larger.

Quote:
Originally Posted by Good advice
Don't play pointer-tricks. Use clear, simple code.
 
Old 11-27-2006, 06:04 PM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by sundialsvcs
...
Thus:
Code:
  union {
    int   i;
    float f;
  }
  f := 100.0;
  printf("%d\n", i);
will do the same thing, but safely.
Some fixes, to allow this sample to survive compilation:
Code:
 union {
    int   i;
    float f;
  } u;
  u.f = 100.0;
  printf("%d\n", u.i);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
About pointers RHLinuxGUY Programming 4 11-15-2005 04:47 PM
pointers in c++ marios_auth Programming 1 06-16-2004 08:20 AM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM

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

All times are GMT -5. The time now is 01:45 PM.

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