LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C pointers : What will be the output? (https://www.linuxquestions.org/questions/programming-9/c-pointers-what-will-be-the-output-504443/)

duryodhan 11-24-2006 06:34 AM

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?

introuble 11-24-2006 06:47 AM

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.

duryodhan 11-24-2006 07:12 AM

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?

introuble 11-24-2006 11:50 AM

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.

paulsm4 11-24-2006 12:32 PM

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;
}


duryodhan 11-24-2006 10:53 PM

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);

nadroj 11-24-2006 11:04 PM

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.

duryodhan 11-24-2006 11:30 PM

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?

introuble 11-25-2006 12:42 AM

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].

jlliagre 11-26-2006 03:26 AM

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.


exvor 11-27-2006 10:40 AM

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.

jlliagre 11-27-2006 01:00 PM

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

exvor 11-27-2006 01:42 PM

I was looking at *p=100; but i was mistaken so nevermind :p

sundialsvcs 11-27-2006 05:34 PM

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.


jlliagre 11-27-2006 06:04 PM

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);



All times are GMT -5. The time now is 05:22 PM.