Address Of Integer

This is a discussion on Address Of Integer within the C Programming forums, part of the General Programming Boards category; Hello, I'm slightly confused with Address aspect. Code: int i=3 int *pi = &i ^ This means that *pi = ...

  1. #1
    Air
    Air is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Address Of Integer

    Hello, I'm slightly confused with Address aspect.


    Code:
    int i=3
    int *pi = &i
    ^ This means that *pi = 3

    Code:
    int i=3
    *i
    ^ For above, *i would mean the address?


    My question is, what does the * mean? Thanks in advance, Air.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    2 different meanings.

    In declaration, * following the type means you are declaring means declare a pointer to that type.

    A * in front of a pointer means dereference the pointer.

    Code:
    int i = 3;
    int *ptr = &i; //declares a pointer to an int, and assign the address of i to it
    printf("%d", *i); //prints 3 (value stored at the address pointed to by the pointer)

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    *i is "undefined" -- it is the value of the int in memory address 3, which may not even exist, and who knows what's in it even if it does.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    No, it's illegal, because i is not a pointer.
    Consequently, pointers can also be declared as:
    Code:
    int i = 3;
    int* ptr = &i; //declares a pointer to an int, and assign the address of i to it
    printf("%d", *i); //prints 3 (value stored at the address pointed to by the pointer)
    (Note the position of the *.)
    Last edited by Elysia; 01-15-2009 at 01:21 PM.
    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. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Elysia View Post
    (Note the position of the *.)
    Which position? The one that screams "I am a C++ coder, and I love it!" ??

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    I'd prefer to think of it as the style that is under-appreciated on this board and which makes most sense
    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. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    I prefer to have it my way and I think that makes most sense... but let's not argue about it in this thread and confuse the OP.

    To OP: Either mine or Elysia's code will work. For now just pick one and stick to it. It's just a stylistic issue and doesn't actually affect the functionality of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. modifying help plz ( int to float )
    By sh4k3 in forum C Programming
    Replies: 11
    Last Post: 06-29-2007, 06:46 AM
  3. Block address from word address
    By xddxogm3 in forum Tech Board
    Replies: 0
    Last Post: 04-25-2007, 09:02 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM

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