Thread: Im so lost at . .

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >what is the difference between & and *?
    & retrieves the address of a variable. It says "give me the address of the variable, not the contents".
    * dereferences a pointer. It says "go to the address contained in this variable and give me the contents of that address".

    Run this and play around with * and & to see what they both do.
    #include <stdio.h>
    int main ( void )
      int i = 10, *p = &i;
      printf ( "& on i: %p\n", &i );
      printf ( "* on p: %d\n", *p );
      return 0;
    My best code is written with the delete key.

  2. #17
    Join Date
    Sep 2001
    > void main(void)
    Not to nit-pick, but even in the most shortest, simplest example codes,
    try not to get into this habbit at all. Just stay away from it!
    Best of luck.
    The world is waiting. I must leave you now.

  3. #18
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    i think mentioning scanf has confused you, scanf is a library function don't forget, its a function thats been written by some egg head to do a certain job. its written to want the addresses of the identifiers that recieve the input from the keyboard. this is so the function can affect these identifiers from within the function, storing the converted data at the addresses given in the function call. don't let that put you off, mebe shouldn't have mentioned it.
    its just written to use the &, so don't worry about it, just remember to type the &.

    the * and & are designed for use with pointers,
    the *(indirection) returns the value stored at the address, the & returns the address
    (but remember you use the * to declare a pointer)
    int *ptr; (create pointer to store the memory address of an int)
    int i=200; (declare int, assign value 200)
    ptr=&i; (assign ptr the address of i, ptr points to i)
    printf("%d", *ptr); (print value stored at the address ptr)

    or u could assign a value directly-
    int *ptr;
    int i;

    *ptr=200; (store 200 at the address ptr points to)

    now an array name is actually the memory address of the 1st element of the array.

    so if you have
    int primes[6]={1,2,3,5,7};

    declare a int pointer ie int *ptr;

    then you assign the address of the 1st element in the array to the pointer

    ptr=primes. (you don't need the & cos array name is an address)

    and to print the value of the 3rd digit, its

    dunno if i'm helping or confusing, i'm sure somebody more qualified will post something m,ore enlightening soon.....

    [edit] they already have !! i'll keep out. the thing with the arrays is worthy of note tho.
    Last edited by stevey; 05-14-2002 at 01:43 PM.

  4. #19
    Registered User
    Join Date
    May 2002

    understanding pointers

    read this suggestion
    understanding pointers

    forget the word pointer for a moment.
    consider this.
    int num1;
    here num1 is a variable which is an integer.
    which would hold the values of integer type.
    yes we should say it now that num1 is an integer type
    variable which would hold integer type values.
    why only integer type?
    because c is designed that way!
    like paRking for handicapped is for handicapped people,
    you try to park you would be c let us say an error will occur.
    whenever a variable is created it has to have some place in memory in order to a child in mothers womb doesnot cover any space on earth,
    but as soon as child is born it starts to use the space etc etc..
    similarly while writing the programm int num1;will be inside the editor
    but once the programm is made to run it occcupies space on memory.
    now each location or space in memory has some value or some notation by which
    it is known just as houses have numbers.government knows your houses by their numbers.each house has a number.similarly each memory address has a number
    by which it is the system or operating is a habit of people that espacially rich people that they will give their houses some
    names like golden_palace or peace or richmans_villa.this is giving names to the a house having number suppose 1234 is also known as golden_palace.
    similarly the memory location is known by its number given by the computer
    or system and the name given by us.
    so in this case letus say NUM1 is the name of the memory and its number is other words yes it is right to say that memory location
    1002 has the name 1002 would be know as the address of the memory location named NUM1.
    when you write a programm you know the variable you create by their names.
    not by what memory location they would be holding. because when you will run the programm which memory location will be assigned to the variables by the system u wont know.but sure you have given the names to the variables which will be created when u run the programm ,that is the memory locations which will be assigned have been named by parents usually start to think of the
    names they are going to give to their kids if it is a boy or a girl.and then they reach a conclusion and final a name say JACK for boy and JILL for girl.
    what if you wanted to know that the variable NUM1 created by you is holding
    which memory location.just like if a mother wants to know the house number
    of her son without telling him ofcourse.she would look for his name in the telephone directory if it has the house number.something like this
    what would you do to find the memory location of their anyway to find
    it out,some technique!,some function,some method!
    ALLRIGHT if you want the address or the memory location number to be printed
    on screen you can use address of operator '&' like this
    okay for a moment the problem is solved,you would be watching the address of
    NUM1 on screen.but what if you wanted to use this address and store its value
    to use it somewhere in the programm or otherwise too.
    what would you do now
    when ever you have to hold anykind of value you decalre a variable of that type.for example if you have to hold 'Z' you know it is character type
    so you will declare a variable of character type say;
    char ALLRIGHT now in ALLRIGHT you can hold 'Z';like
    as we have decalred NUM1 in the begging of our explanation int num1;
    to hold its address there has to be a address we have address book in our homes where we write the address of people we want to.
    in C too there is a facility of holding these address of variables.
    this address holding facility is named POINTER.
    but what is a pointer?
    POINTER is simply an address holder which holds the address of variables.
    but how?
    a variable is declared or made like int num2;
    we know this would hold integer values
    in order to make this hold addresses
    '*' is prefixed to num2 making it a to make int num2 a pointer we write it like this
    int *num2;
    this is telling the compiler that NUM2 which is an integer variable
    will now act as something which holds the address of variables.which are of
    integer type;just like we have diffrent pages in address book marked alphabatically for our convienence and we write all about ALLRIGHT on page makered A and MADONA on page marked M.
    so why int *num2 will hold only the addresses of integer type variables? it is
    simply for the sake of convienece of bookeeping by the system so it is known to it that if a pointer is int type then it would hold only addresses of integer type.
    again let us consider this thing
    int num=1;
    int *num1;
    what is the diffrence between the 1 of num and num1;
    in case of num it is just an real number 1. just ordinary number 1 which could be divided multiplied etc.etc.
    but in case of 1 of num1 (num1=1) here 1 means a memory location in computer
    numbered 1 or having the address 1 or marked 1.
    again consider this
    we know num1 is a pointer so it can store the address of integer type num is an integer variable in our case.
    putting '&' infront of num has made the compiler to understand it as
    address of num.that is to say when ever &num statement is encountered
    compiler will translate or understand it or read it as 'address of num'
    because & means 'address of';and it is called also address of operator in this when compiler will encounter num1=&num;
    what would it do or how will it comprehend this statement ?
    like this
    compiler`s comprehension:-
    store the 'address of'num into num1;
    now inorder to store the value of num in num1
    it is obvious or evident or resonable to say that num1 must have place of itself in the if you go to market and buy a bed and tv and lots of other things.then it would be wise for you to have a room too, to store them or not? or would you like them to keep them in open and even if in open
    then that land on which you are going to keep it all has to be yours otherwise govenrment wont let you do that and if you try to put it on somebody elses property you are sure to have a quarrel or fight in which govenrment and ALLRIGHT would be on the side of the owner of the land.
    so you wont have any chances.
    similarly in order to store the address of num in num1 first num1 must have some memory location assigned or sanctioned or provided to num1 itself
    well yes that is done it has a memory location for itself as soon as you ran the programm and control came over int *num1;it was assigned the memory location at that very moment.let us say the memory location given to num1 is
    numbered 800.
    now this location 800 is having the name num1 and what should be stored in it
    the address of NUM say the address of num is at the location 800
    which is named NUM1 would be stored that is inside 800 would be stored
    the value 700 which is named more thing anything you store in
    a POINTER is always understood as adress of something that i dont want to convey you that you can store 'a' in num1 when i say anything
    it should be some integer value only.
    same goes for other types of pointers be it float ,double,character
    structure. all these kinds of pointers will only be holding addresses
    even if the pointer is something like this
    char *ALLRIGHT;
    char okay='n';
    ALLRIGHT thou is a charater variable but it is not only a charater variable
    it is a address holder of a character variable.that is it is a pointer.
    so if you write
    it would result in an error;it cannot hold anything else but numerical values
    which are address;
    same goes for al kinds of pointers.they can only hold numerical values which are the address of the same kind of variables as they are;like int *num1 can only hold addresses of integer type variables;
    so it would be safe to say now that
    POINTERS are variables just like any other variable but with one speciality
    or property or attribute that they will hold address of variables of same kind as they themselves are.
    what is &?
    when relating to pointers this is an address of operator.
    int num=1;
    int lum=2;
    int *num1;
    num=lum;//this means lum is holding 2 as value in its own location or address
    which should be copied to the location or address of num
    so now num==2;
    both lum and num are 2 now.
    num1=&lum;//this means not the value which the location or address of lum is holding but the address itself or the location number itself of the lum
    should be copied to the location or address of num1;
    so this is what * and & does?
    i have tried and little less than my best to explain pointers and address here.if i have failed to make you understand please dont mind
    i am just a student ;
    correct me if i am wrong
    i myself need the help of your esteemed self;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The getting lost and walking in circles
    By Mario F. in forum A Brief History of
    Replies: 26
    Last Post: 05-07-2008, 09:53 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM