ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054
Rep:
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?
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054
Original Poster
Rep:
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?
// 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;
}
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.
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054
Original Poster
Rep:
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?
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].
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.