Thread: Struct help...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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, nort, etc.

  2. #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.

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #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?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #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!

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm telling you, I want to copy the data into the variable itself, and not what it points to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM

Tags for this Thread