Intro to pointers in C/C++ tut
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Intro to pointers in C/C++ tut

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    344

    Intro to pointers in C/C++ tut

    i am planning on writing a turtorial on pointers in C/C++ because i think that there are many newbies out there with questions. This tutorial wont cover advanced pointers, but mainly, my goal is to show newbies how to use pointers in C/C++. Please let me know if you have any ideas of what i should include.

    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #2
    Senior Member
    Join Date
    Feb 2003
    Posts
    282
    Well I get confused with functions that return pointers, and pointers to functions. Not sure if thats advanced. I also get confused with when to use the * operator. I know basic pointers like

    int *ptr;
    int a = 100;
    ptr = &a;

    printf("A integer %d", *a);

    But also character arrays confuse me.

    char *array = "Hello World";
    sprintf("a string %s", array);

    Part that confuses me is why array gives me the string, and why with an int pointer *ptr gives me the integer.

    But then pointers to pointers realy get me. Like a data structure the contains a pointer to another structure.

  3. #3
    Senior Member
    Join Date
    Feb 2003
    Posts
    118
    Pointers are not as difficult as people think.

    int a=100;

    with that you declare an integer with a value of 100. To have its address you use the & operator

    printf( "the value: %d -- the address: %d \n", a, &a);


    If you want to declare a pointer you must use the * operator

    int *int_ptr;

    All you have to know is if you want the value you must use *int_ptr and if you want the address you must use int_ptr (this is a short of &(*int_ptr) ).
    So if you want pointing int_ptr to the address of a
    int_ptr = &a; /* address of int_ptr == address of a */

    if you want the value
    printf( "value of a: %d -- value of int_ptr: %d\n", a, *int_ptr);


    This is exactly the same for the string so when you write
    char *array = "Hello World";
    sprintf("a string %s", array);

    array point to the address of the string "Hello World" so the print function write all the char beginning at the address array and end when the char \0 (end of string) is found.

    a string is an array of char
    char MyString[] = "Hello World";

    the address of the array start at the address of the first char: &MyString[0] but this is the same as writing MyString so

    char *ptr_string1;
    char *ptr_string2;

    ptr_string1 = &MyString[0];
    ptr_string2 = MyString;

    printf( "string1: %s -- string2: %d\n", ptr_string1, ptr_string2);


    Hope this will help people

  4. #4
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    There is nothing at all magical about a pointer, it is just a hex value that 'points' to a specific address in main memory. Since your program will be running (most likely, kinda tossing aside the possibility it is in a register) in main memory and your variables will be out there as well, you can reference any of the variables by simply pointing to their assigned spot in memory (the C compiler automagically figures out how many bytes of memory to read from that point by looking at other things, like the type (ie, int, char, etc)).

    So, int *ptr = "Hello world"; will assign the memory address for where the string "Hello World" is stored and move the address into the variable ptr. And yes, int should work since char is actually the same as int in C

    '&' in the instance you use it means to return the memory address for where the variable a is stored. Since ptr is a pointer to an address, and you said it is equal to the memory address of a, then if you were to check the value of *ptr, it would be the same as a, or 100

    Need to be careful not to get & confused with passing variables to functions by reference though...they are very similar but slightly different concept, but work on the same principal.

    Hope that helps,

    /nebulus


    EDIT: Heh heh, damn just got beat
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  5. #5
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Well I get confused with functions that return pointers
    First of all, if i remember right, functions are NOT suppose to return pointers, because when they go out of scope, the address the pointer was pointing too gets filled up with something else...So that defiently not good. Remember i said i wanted to keep this tutorial simple because it is the first one i am writing. I will cover basic usage of the * operator, since people sometimes get confused when to use it and when not to use it.

    But also character arrays confuse me.
    Good i will cover pointers pointing to arrays.

    But then pointers to pointers realy get me. Like a data structure the contains a pointer to another structure.
    I think you are talking about inheritance. Pointers inside of a base class pointing to the original members base class members within the derived classes. This is advanced and will definetly not be covered in my tutorial.

    by the way, can i attach a .doc file to my post? it isnt listed bellow as one of the files that you can attach...i am planning on just writing the tutorial and then letting the people download the .doc file and read it.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    344
    ok, i finished the tutorial it took me 2 hours and it is 5 pages long. I wrote it using mircosoft word. Autoformat nearly killed me along with spell check, but in the end, i ended up turning them off. I am planning on posting the tutorial tonight or tomarrow.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  7. #7
    Junior Member
    Join Date
    Jul 2003
    Posts
    7
    Pointers can get confusing from time to time, I will make sure I check out your tutorial when/if you post it.

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Thank You
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  9. #9
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    if you need some programs for an example you can download them from my site www.infantryghost.com/Programming.html theses are all mine that I have built. I have some on arrays that I think should help you out.
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  10. #10
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    First of all, if i remember right, functions are NOT suppose to return pointers, because when they go out of scope, the address the pointer was pointing too gets filled up with something else
    This is inaccurate. It is very very common to return pointers from functions; for example, a pointer to a struct or c++ object being returned from a function is very common. You wouldn't be returning the pointer to the function's location but rather the struct's location in memory (but things like this are why you have to be careful with pointers), and always be sure not to leave dangling ones


    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •