C++ Help.

Discussion in 'Computer Gaming Forum' started by Twimfy, Mar 22, 2009.

  1. Twimfy

    Twimfy Site Supporter 2015

    Joined:
    Apr 10, 2006
    Messages:
    3,570
    Likes Received:
    32
    Could someone please help me understand what is happening here:

    // arrays example
    #include <iostream>
    using namespace std;

    int billy [] = {16, 2, 77, 40, 12071};
    int n, result=0;

    int main ()
    {
    for ( n=0 ; n<5 ; n++ )
    {
    result += billy[n];
    }
    cout << result;
    return 0;
    }

    I am following a tutorial on C++ and doing quite well at it but I always get stuck on arrays.

    The output of this is 12206 but I can't work out why, the tutorial usually explains everything or I can usually work it out for myself but here I can't get my head around it.

    I understand that 12206 is the sum of the array contents but I don't know how that happens.

    It looks as if the for loop declares the size of the array as zero (n=0) and then increments the elements by one (n++) and then when it reaches the condition of no more than 5 elements (n<5) passes the value of Billy [n] (where n would be 5) to using the += (right to left operator) to result which is then displayed.

    If I'm right (and please correct me if im not) then why does the example code make an issue of declaring the number of elements within the array of as 0? I thought by default C++ would automatically assign the number of elements based on the initialization values.
     
    Last edited: Mar 22, 2009
  2. kammedo

    kammedo and the lost N64 Hardware Docs

    Joined:
    Sep 24, 2004
    Messages:
    2,138
    Likes Received:
    12
    Not the size of the array, but the n "cycle" variable is initialized to 0 (= first index in the array).


    Actually that is only true if you dont initialize an array to a given size (which is your case, => int billy []) , for example a three cells array
    MUST be initialized as
    int billy [3] = {16, 2, 77};
     
    Last edited: Mar 23, 2009
  3. Twimfy

    Twimfy Site Supporter 2015

    Joined:
    Apr 10, 2006
    Messages:
    3,570
    Likes Received:
    32
    Ok, thanks I understand it now.

    Now I just have to get my head around multidimensional arrays and pointers.

    I get how pointers work and can use them in examples but at the moment I don't really get why they are used.

    If an interger called 'a' stores a value of lets say 25 at address 2000 then why is it necassary to have a pointer to address 2000 why not just call 'a' somewhere else in the code?

    I'm not having too much trouble learning C++ but many books and tutorials don't give many real world examples or uses.
     
    Last edited: Mar 23, 2009
  4. mairsil

    mairsil Officer at Arms

    Joined:
    Apr 20, 2005
    Messages:
    3,425
    Likes Received:
    153
    There are many reasons for pointers, and it would take too long to try to explain all of the functions. For a beginner in C++, I will say one of the earliest uses of pointers might be for dynamic arrays (arrays whose size is not known until runtime).
     
  5. Dreamcast

    Dreamcast Intrepid Member

    Joined:
    Jul 17, 2007
    Messages:
    619
    Likes Received:
    35
    I also had some trouble with pointers until I figured out all of the great things they can do. As Mairsil pointed (;)) out, you can use a pointer to point to a dynamically allocated area of memory to create arrays at runtime. With a static variable assigned at runtime, it points to a statically allocated area of memory which cannot be changed size-wise.

    The most useful feature of a pointer for me is the ability to typecast, or make a variable think it's of another data type. Let's say that I have an array ({1,2,3,4,5}) of type "unsigned character." Each entry in the array can be a number ranging from 0 - 255, so 256 possible combinations. If I create a pointer of type "unsigned short", I will have an address space of 2 bytes (16-bits), or 65536 possible combinations. When pointing to the unsigned character array with my unsigned short pointer, I can make the program think every two character entries is actually a single unsigned short. So, instead of entry 1 being '1' and entry 2 being '2', they would actually read: '258' (depending on the endianess of the machine you're running the app on).

    Here is the code:

    - - - - -
    unsigned char a1[] = {1,2,3,4,5};
    unsigned short *p = (unsigned short*)a1; // the code before 'a1' typecasts a1 to the same datatype as the pointer
    cout << p[0] << endl;
    - - - - -

    Another real-world example is receiving raw data from a network packet. You can create a structure with entries like IP, port, and TTL, then point to the RAW data with a pointer of type structure. Your data remains the same, but the application can read / modify it in an easy to access way.

    Those are just a few of many uses for pointers. The fun thing about pointers is figuring out different ways to use them. The most important thing to remember about pointers is they do exactly what their name suggests: they point to things. So, they aren't anything themselves, they just tell you how to get there.
     
    Last edited: Mar 23, 2009
  6. Twimfy

    Twimfy Site Supporter 2015

    Joined:
    Apr 10, 2006
    Messages:
    3,570
    Likes Received:
    32
    :eek:h: That all seems way over my head at the moment but hopefully it will become useful in time.

    I'm really disappointed with myself, I seemed to be progressing at such a nice pace and then bam! it all seemed so complicated all of a sudden.

    I've just started dealing with this example:


    int main ()
    {
    int numbers[5];
    int * p;
    p = numbers; *p = 10;
    p++; *p = 20;
    p = &numbers[2]; *p = 30;
    p = numbers + 3; *p = 40;
    p = numbers; *(p+4) = 50;
    for (int n=0; n<5; n++)
    cout << numbers[n] << ", ";
    return 0;
    }

    I was completely confused by what it did and then I took upon myself to do this:

    int main ()
    {
    int numbers[5];
    int * p;
    p = numbers; *p=10;
    p++; *p = 20;
    p++; *p = 30;
    p++; *p = 40;
    p++; *p = 50;
    for (int n=0; n<5; n++)
    cout << numbers[n] << ", ";
    return 0;



    }

    Which is a much simpler version, I understand why the previous version was used as an example as it was just trying to highlight the different methods that can be used but for me it was too much too soon. Many of the tutorials seem to be set up in a way by which they presume you know a lot more than you do.
     
  7. selgus

    selgus <BR><IMG SRC="http://assemblergames.com/forums/ima

    Joined:
    Mar 8, 2008
    Messages:
    84
    Likes Received:
    3
    It would have been nice in their tutorial, for them to comment what they are doing, and why they were equal. When teaching, you should really try and stress why something is being done a certain way, and the pros/cons of doing so.

    I am also not a big fan of single operation constructs not using brackets: i.e. that "for" loop, not being written:
    Code:
    for (int n = 0; n < 5; ++n)
    {
             cout << numbers[n] << ", ";
    }
    
    as it become clear what is being iterated on, along with helping catch bugs if/when the code is modified.

    As for the code about typecasting different datatypes with pointers: be careful about what you are pointing at and the restrictions of the processor you are on. There are alignment issues, and many "defaults" are compiler implementation dependent. Like on the MIPS, if you point a short (16-bit) pointer at a char array (8-bit) and that array happened not to fall on a 16-bit boundary, if you dereference that pointer, you will get an exception.

    Though when making global arrays like in the example, most compilers will align the start of the memory to the natural alignment of the processor. Just something to keep in mind with embedded data in classes and such.
    --Selgus
     
  8. mairsil

    mairsil Officer at Arms

    Joined:
    Apr 20, 2005
    Messages:
    3,425
    Likes Received:
    153
    If it helps, you can take a look at the notes I prepared for a lab I taught a few years ago:

    http://mouse.pouncingkitten.com/csci103/

    Realize that these were my working notes; the lab was much more hands-on with a lot of explanation by me.
     
  9. Twimfy

    Twimfy Site Supporter 2015

    Joined:
    Apr 10, 2006
    Messages:
    3,570
    Likes Received:
    32
    Thanks I'll take a look.

    My only target is to have a good enough understanding of the language in general so I can decipher some scripting templates in a game engine I use, but I've been having a lot of fun working though the examples and using xcode so I figured I might as well tackle as much as possible.
     
  10. retro

    retro Resigned from mod duty 15 March 2018

    Joined:
    Mar 13, 2004
    Messages:
    10,354
    Likes Received:
    822
    If you can afford it, get yourself on a night course. That's really worthwhile. If not, get yourself a good book on the topic - that's the next best thing!
     
  11. kammedo

    kammedo and the lost N64 Hardware Docs

    Joined:
    Sep 24, 2004
    Messages:
    2,138
    Likes Received:
    12
    Taken with very large views, everything is a "pointer" in C / C++. And I mean everything - If you accept that address = pointer, that is.
    Once you understand this basic concept of C, thou hast ready to behold tee mighty power of C in thy hand! :)
    Keep on going, you will get much cleverer by using it and making experience than by just passing an exam.

    The next step is to know how to put those basics to good use, which means knownig Windows or Linux/Unix or whatever obscure os you are working on. Personal experience speaking here!
     
    Last edited: Mar 23, 2009
sonicdude10
Draft saved Draft deleted
Insert every image as a...
  1.  0%

Share This Page