Thread: Getting a grasp on pointers

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >@ Prelude it seems like when you answer a thread, trouble starts.
    It happens occasionally. In my experience, it's usually someone who thinks he knows a lot (always a he, by the way) and gets corrected by me. Depending on the self-esteem and/or comfort level of the person in question, actual dislike starts immediately after I "humiliate him in public" or after he finds out that I'm a girl.

    People like that either get bored or banned after a little while and rarely return.
    My best code is written with the delete key.

  2. #17
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    hey prelude. I've been out of here for the most part for a few months. I have to admit, I've noticed since I've been back that you've gotten a bit more abrasive and short with people. Just an observation. Drunk with power eh?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #18
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    errr.... I was wrong. Get 'im prelude
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #19
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Edit: Oh dear, the following turned out quite long... Much longer than I expected...
    Edit 2: Always nice to see proud, stuck-up children hijack threads...


    OP:
    First of all, to fully understand how pointers work - and inevitably, why they're used - you should first have an understanding of how the computer store stuff in memory.
    Now, you may or may not already know this, but this topic is renowned as difficult, and maybe a little daunting, for beginners to low level programming languages, so I'll just write this down and maybe a few others reading this will learn something.


    Think about the memory (RAM, harddrive, whatever) as a piece of paper with a checked pattern, like the ones you might have used in elementary school when studying mathematics. One of these squares is the smallest amount of memory you can access, and is called a bit.
    A single bit can only take on the numerical value 0 or 1. So if we want bigger numbers we'll have to claim access to a chunk of bits. The following names refer to different sizes of such chunks:

    byte, consists of 8 bits grouped together.
    word, 16 bits grouped together - i.e. 2 bytes.
    double word, 32 bits, as the name suggests, 2 words, or 4 bytes if you so will.

    In C++ there are many variable types you can use, these include char, short, int, float & double.
    These predefined types all have a set amount of bits allocated for them. char takes up 1 byte, short takes up 1 word (remember, 2 bytes). The size for int is system dependant, but the most common today (32bit systems, like Windows 95+) is 2 double words (i.e. 2 words, i.e. 4 bytes).


    So, when you define a variable in your program, like so:
    Code:
    char my_var;
    then the my_var variable is in charge of the size 1 byte takes up - i.e. 8 bits.
    So, we have 8 bits we can play around with... Let's store the number 14 in our byte:
    Code:
    my_var = 14;
    What happens here is that the number 14 is stored at our variable's address (place) in memory.

    Another thing we can do is assign our variable a value of another variable:
    Code:
    char another = 99;
    
    my_var = another;
    This would make "my_var" contain the number 99.

    So, to the left of the assignment operator (equation sign) the adress is used, and to the right the value (content) is used.
    Aha!

    Now, C++ lets you manually "extract" the address and use it as if it was a value.
    This is done using the address-of operator like this:
    Code:
    my_var = &another;
    Now "my_var" will contain the address of the variable named "another". This will in theory work since an address is a numerical value...
    The reason I say it will only work in theory is because
    A) the compiler won't let you do this assignment (more on this later), and
    B) the address of a variable will not fit in 1 byte. Infact, it will be a number taking up 4 bytes (same as an int), but let's not worry about that now.

    Remember, after the operation above, the variable "my_var" contains the address of the variable "another" (in theory, but bear with me). But what if we want to get the value contained by "another" through "my_var"?
    It should be possible, right? I mean, we have the address of the variable we want to get the value (content) of...

    Yes, C++ has another operator you can use for this. It is called the de-referencing operator and simply returns the value located at an address. Perfect.

    So, if we do the following:
    Code:
    char anothers_value = *my_var;
    then the variable "anothers_value" will infact contain the value of "another".


    Now to the reason why the compiler wouldn't let you assign the address of "another" to "my_var".
    The compiler would only allow this if "my_var" would have been a pointer to a char. Yep, pointers point to variables, and will thus contain the address of another variable. I.e. its value will be another variable's address...

    So, how do we define a pointer? Quite simply, like this:
    Code:
    char *my_pointer;
    After this, we can make "my_pointer" point to some other variable that is of the type char.
    For example, we could write this:
    Code:
    char my_var;
    my_pointer = &my_var;
    And if we later wanted to change my_var, or get its value, we could do so through the use of "my_pointer".

    Actually, if we did like this:
    Code:
    *my_pointer = 50;
    then "my_var" would get the new value 50.
    This works since we used the de-referencing operator on my_pointer, to extract the value it contains - which is the address of "my_var"!

    (Remember, to the left of the assignment operator (equality sign) the address if used (in this case, the address of "my_var"), and to the right the value is used (the value of 50 here).)

    But wait! I said earlier that the address of a variable takes up 4 bytes!
    Yes indeed I did, and infact, every pointer takes up the same space in memory as an int (i.e. 4 bytes).
    Thus, we can safely make "char pointers" for example. So, in the code above, "my_pointer" takes up the size of of an int, and "my_var" takes up the size of a char.


    When should we use pointers?
    As both Prelude and Mario F. implied, you will probably yourself eventually come up with situations when you want to use pointers if you just sit down and write code.
    However, I could always list a few uses here, because maybe they will make more sense then...

    If you have two variables, named simply "A" and "B", and say you want to change the value of either depending on some conditional, otherwise change the other, then you could do like this:
    Code:
    int A = 2;
    int B = 3;
    
    if (some conditional...){
    	A = 10;
    
    } else{
    	B = 10;
    }
    However, pretend more things need to be done to the variable (not just settings its value to 10, but maybe also print it etc), and you decide to put that execution in a function, then you could do like this:
    Code:
    void change(int *P){
    	// first we print P using whatever method
    	...
    
    	// now we want to change its value to 10
    	*P = 10;
    }
    
    
    // and at some other place in our code we have the conditional:
    
    int A = 2;
    int B = 3;
    
    if (some conditional...){
    	change(&A);
    
    } else{
    	change(&B);
    }
    So, the function "change" has one argument, which is a pointer to an int, and when we call it, we give it the address of either "A" or "B".
    Very much like the previous code, in which we did this:
    Code:
    char *my_pointer = &my_var;
    remember?


    I hope this "essay" helped out, in some way or another... =)
    Last edited by Osaou; 07-26-2006 at 01:05 PM.

  5. #20
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Sorry for going off topic, but I just had to add my 2 cents before this gets locked.

    >>I guess that Prelude has little time for imagination, because being specific
    requires some general knowledge.

    I've been watching from behind the scenes, eating popcorn and enjoying the
    show, but I still don't get this comment - lilrayray's comments were very vague,
    as is the case when you're not sure why you're not getting something, but do
    you expect Prelude to "imagine" a question to answer? I find that funny, but
    maybe that's just me...

    >>What good does "wrong" suggest, if it is not supported by the correct
    answer?

    A valid question, but in some cases, a person will be so profoundly wrong in
    their thinking that in order to explain why they are wrong would take such
    an inordinate amount of the second (and third in this case) parties' time that
    they're head might explode, or at least get a little frustrated.

    "Never argue with a stupid person - they bring you down to their level and beat
    you with experience" - too true

    I think that we should add to the FAQ a list of notable members who you would
    be out of your mind to argue with unless you have irrefutable evidence that they
    are wrong - God help any poor noob who takes on quzah! Alas, such individuals
    likely to make that mistake don't read the FAQ!

    >>I will not post any more to this thread, and by God I will not force your hand
    to type anymore.

    Aw you promised, but then there's this:

    >>I'd like to see you correct Salem, but not the spanking sort of correction, but I
    mean in code terms; you can save the spanking for us.

    I recommend neutering - "fixes" behavior that'll get you into trouble!

    @ MD: Don't take too much offense to what I've said here, I'm just amusing
    myself - it's just I've seen people go up against those more knowledgable than
    them on this board before and loose - every time. I accept that I've not given
    answers to your question of why your concepts are wrong, and that's for two
    reasons:

    1. I don't know enough about internal memory management to give a thorough
    answer, say to the standard of one Prelude, or Mario F, or Salem (list goes on),
    2. They didn't feel the need to, and neither do I.

    I will direct you here, and here, if you do want to learn more.

    On a final note, I second Mario's call to close, the OP's curiosity seems to have
    been resolved to a some degree, which is a good thing, and keeping the
    thread open will only allow the flaming to continue.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #21
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    @Osaou- Thankyou very much for spending the time to help me. I already knew how to declare the pointers and obtain the adress of another variable, but the last part of its uses sparked a couple ideas and now I think I understand it better. Thanks a lot.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MDofRockyView
    I would hate to see any more people voluntarily flame themselves, instead of providing any truth or knowledge. So I will stand by my answers until such time.
    You want 'short'? You're a ........ing idiot. Every single thread you post in suffers a hit on intelligence. I'd say you were fischer, but he couldn't help saying stupid ........ like how he wrote the C language, or some other ridiculous banter. You're stupid on a fischer-level.

    Oh, and just because no one feels your pointless thread clogging ........ is worth replying to does not make you right, you ........ing moron.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'm torn between seeing how things turn out and nipping it in the bud. I think I'll go with the latter. MD, if you're really that interested, start a new thread and ask your question.
    My best code is written with the delete key.

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by lilrayray
    Hello all, I am quite new to C, and Iam trying to learn pointers. I know how to declare them and what not, I am just confused on their uses. Does anyone know of any examples that might give me a better understanding? I read the article of pointers on this site and it was of some help, but I am still a little confused.
    A TUTORIAL ON POINTERS AND ARRAYS IN C
    The Function Pointer Tutorials
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM