C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2008, 11:52 AM   #16
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
Quote:
Originally Posted by Elysia View Post
Because, simply put, floats are not stored the same way ints are. That's why.
Actually, I think the answer the OP was look for is this:

Given this:
Code:
int n = 1;
int *p = &n;
n contains 1, but p doesn't contain 1. p contains the address of the variable n in memory. This might be 0x345dee4 (memory addresses are typically written in hexadecimal), or whatever. It might change every time you run your program.

Regardless, you shouldn't care about the exact value that p holds -- you know that it "points" to n, and that's good enough.

*p, on the other hand, tells the compiler to look at p and find the address it holds; and then look at the memory at that address. In effect, it's the same thing as looking at n directly. And thus, *p == 1.
Code:
#include <stdio.h>

int main() {
    int n = 1;
    int *p = &n;
    printf("n: %d\n", n);
    printf("p: %p\n", p);
    printf("*p: %d\n", *p);
    return 0;
}
Output of one run on my system:
Code:
n: 1
p: 0x7fff6017f514
*p: 1
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-02-2008, 03:53 PM   #17
Registered User
 
Join Date: Oct 2007
Posts: 242
Doesn't (int *)&n (assuming n is an int) supposed to return a pointer to a pointer (**)?
I mean, you are casting the address of n into an int pointer - or what exactly happens if I cast something into a pointer?

Last edited by eXeCuTeR; 07-02-2008 at 04:24 PM.
eXeCuTeR is offline   Reply With Quote
Old 07-02-2008, 04:39 PM   #18
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
Casting doesn't do anything most of the time (except in some circumstances). All it does is tell the compiler you know that you're treating an object of one type as an object of another one, when in fact you know such treatment is considered breaking some safety rules, but you want to do it anyway.
__________________
MacGyver is offline   Reply With Quote
Old 07-02-2008, 05:48 PM   #19
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by eXeCuTeR View Post
Doesn't (int *)&n (assuming n is an int) supposed to return a pointer to a pointer (**)?
I mean, you are casting the address of n into an int pointer - or what exactly happens if I cast something into a pointer?
&n is float*, the address of a float variable, not a pointer to pointer.
Consequently, float* is not int*, therefore I use a cast to turn that float* into int*.
Casting turns the type on the right side into the type on the left side. It does not take the address.

Look at this:
Code:
#include <stdio.h>

int main() 
{
	float f = 1.0f;
	int* n;
	memcpy(&n, &f, sizeof(int*));
	printf("%p\n", n);
	n = (int*)&f;
	printf("%p\n", n);
	printf("%i\n", *n);
	printf("%f\n", *(float*)n);
	return 0;
}
The output is:
3F800000
0012FF60
1065353216
1.000000
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-02-2008, 08:23 PM   #20
Registered User
 
Join Date: Oct 2007
Posts: 242
Why are you passing &n and not n? &n is a pointer to a pointer, isn't it?
---
So what's up with (int *)&f?
eXeCuTeR is offline   Reply With Quote
Old 07-03-2008, 04:08 AM   #21
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
If you're referring to...
memcpy(&n, &f, sizeof(int*));
...then what you don't seem to understand is that a pointer is a variable, and by taking its address, I can put a value into it.
The memcpy is the same as...
int* n = (int*)f;
...although the compiler will not allow it.

And give yourself a break and go back to the books. We've already explained the last one.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-03-2008, 02:44 PM   #22
Registered User
 
Join Date: Oct 2007
Posts: 242
Quote:
Originally Posted by Elysia View Post
If you're referring to...
memcpy(&n, &f, sizeof(int*));
...then what you don't seem to understand is that a pointer is a variable, and by taking its address, I can put a value into it.
The memcpy is the same as...
int* n = (int*)f;
...although the compiler will not allow it.

And give yourself a break and go back to the books. We've already explained the last one.

I know these stuff but my point is the memcpy gets a pointer in it's first parameter while you are passing a pointer to a pointer!
eXeCuTeR is offline   Reply With Quote
Old 07-03-2008, 02:51 PM   #23
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
I'm telling you, I want to copy the data into the variable itself, and not what it points to.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
struct

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm suppose to use this library but I'm confused on what exactly the data structure mr_coffee C Programming 1 12-03-2008 03:10 AM
Global Variables Taka C Programming 34 11-02-2007 03:25 AM
Struct with a ptr to a dynamically allocated array of other structures :( michael- C Programming 10 05-18-2006 11:23 PM
What's wrong with my search program? sherwi C Programming 5 04-28-2006 09:57 AM
Tutorial review Prelude A Brief History of Cprogramming.com 11 03-22-2004 09:40 PM


All times are GMT -6. The time now is 09:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22