Announcement

Collapse
No announcement yet.

Yet more C++ programing help...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Yet more C++ programing help...

    Well only one more week left so don't have to worry about it after then

    heres what I got:

    Code:
    //•     Declares, initializes, displays the contents of pointers to an int.*
    //•     Assigns one pointer to another pointer.*
    //•     Compares pointers for equality.*
    //•     Implements an array of ints using pointer notation and displays array elements by  incrementing a pointer.
    //•     Implements a C-string using pointer notation and displays result of incrementing its pointer.*
    //•     Implements a function that is passed a pointer as an argument (use function prototype). Function definition displays string content using only pointers.
    //•     Creates a dynamic array, assigns it values and displays its content,Using only pointer notation.
    //MS Visual Studio Academic Edition
    
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    using namespace std;
    
    int main() {
         
         int i=10;
        int *j;
    
           j=&i;
    
           cout<<"\n ---------- Addresses of Variables --------- \n"<<endl;
           cout<<"Address of i = "<<&i<<endl;
           cout<<"Address of j = "<<&j<<endl;
    
           cout<<"\n ---------- Values of Variables -------- \n"<<endl;
           cout<<"value of i = "<<i<<endl;
           cout<<"value of j = "<<j<<endl;
           cout<<"value of *&i = "<<*&i<<endl;
           cout<<"value of *j = "<<*j<<endl;
    
           cout<<"\n *j = 5 \n"<<endl;
           *j=5;
    
           cout<<"value of *j after modification = "<<*j<<endl;
    
    
         cout  << boolalpha //compares pointers for equality
             << "The true expression 5 != 2 yields: "
             << (5 != 2) << endl
             << "The false expression 40 == 10 yields: "
             << (40 == 10) << endl;
         
       int *onePtr, *twoPtr, a = 10; //Assigning a pointer to another pointer
         onePtr = &a;
         *onePtr = 40;
         cout << a << endl; // outputs 40
         cout << *onePtr << endl; // outputs 40
         twoPtr = onePtr; // same as twoPtr = &a;
         cout << *twoPtr << endl; // outputs 40
    
         int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 };
         int *i_ptr, count;
         float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 };
         float *f_ptr;
        i_ptr = i_array;
        f_ptr = f_array;
        for (count = 0; count < MAX; count++)
              printf("%d\t%f\n", *i_ptr++, *f_ptr++);
    
           
       return 0;
        }
    I'm getting an error towards the end of the program saying I have too many identifiers and MAX is an undeclared identifier..can anyone help me out with this?

    Also Can someone point me in the right direction to solving these?

    Implements an array of ints using pointer notation and displays array elements by incrementing a pointer
    Creates a dynamic array, assigns it values and displays its content,Using only pointer notation
    Implements a function that is passed a pointer as an argument (use function prototype). Function definition displays string content using only pointers

    Thanks!
    Why is it called tourist season, if we can't shoot at them?

  • #2
    You might try actually defining "MAX", as the compiler can't read minds.

    Code:
    #define MAX 10
    or

    Code:
    const MAX = 10;
    Not sure which is "better," because I don't really know C.
    Last edited by Jon P. Inghram; 13 January 2007, 16:57.

    Comment


    • #3
      It's only been 15 yrs since my last foray into C++
      Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

      Comment


      • #4
        Originally posted by Jon P. Inghram View Post
        You might try actually defining "MAX", as the compiler can't read minds.

        Code:
        #define MAX 10
        or

        Code:
        const MAX = 10;
        Not sure which is "better," because I don't really know C.
        The #define is better. You don't want to use any kind of variable for the declaration of an array that comes off the stack.

        Also, off the cuff, I don't think you can declare the size of an array if you're defining its contents at the same time.
        Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

        Comment


        • #5
          Originally posted by Wombat View Post
          Also, off the cuff, I don't think you can declare the size of an array if you're defining its contents at the same time.
          Just looked it up and you can do it either way.

          GT98, you might try an online tutorial on C++, like http://www.cplusplus.com/doc/tutorial/ or http://www.cprogramming.com/

          Comment


          • #6
            A const (static const) is always preferable it has a scope and a type, a define hasn't.

            You can look at a define as a text replacement (the preprocessor does all the text replacement before the compiler compiles the code), you mainly use defines for macro's or in header files. Also a define (#ifdef #endif) can always be redefind afterwards so another reason to use a const.
            Main: Dual Xeon LV2.4Ghz@3.1Ghz | 3X21" | NVidia 6800 | 2Gb DDR | SCSI
            Second: Dual PIII 1GHz | 21" Monitor | G200MMS + Quadro 2 Pro | 512MB ECC SDRAM | SCSI
            Third: Apple G4 450Mhz | 21" Monitor | Radeon 8500 | 1,5Gb SDRAM | SCSI

            Comment


            • #7
              Originally posted by KeiFront View Post
              A const (static const) is always preferable it has a scope and a type, a define hasn't.

              You can look at a define as a text replacement (the preprocessor does all the text replacement before the compiler compiles the code), you mainly use defines for macro's or in header files. Also a define (#ifdef #endif) can always be redefind afterwards so another reason to use a const.
              On the contrary, you want a define precisely because of the reasons you've described. When you're keeping multiple arrays tied together, the global scope of a #define helps keep things in sync, across classes and cross-linked programs, and you can indeed prevent redefines much in the same way you prevent multiple inclusions - this prevents compilation much the same way that attempting to redefine consts does. Plus, what does the type of a const buy you in the context of an array size? Since standard C arrays cannot change size dynamically, textual substitution makes sense. In fact, the lack of a type may even be beneficial, as you can use the size of the array as a comparison against different types of variables (int, u8, u64, longs...) to see if you can manage the size of the array.
              Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

              Comment


              • #8
                He's using C++ (and you can resize a C array with malloc iirc, my C is very rusty, I mainly programmed in C++), and you want to obey scoping rules so a const is preferable. Also you will learn to value type safety. A define can have nasty side-effects.
                Main: Dual Xeon LV2.4Ghz@3.1Ghz | 3X21" | NVidia 6800 | 2Gb DDR | SCSI
                Second: Dual PIII 1GHz | 21" Monitor | G200MMS + Quadro 2 Pro | 512MB ECC SDRAM | SCSI
                Third: Apple G4 450Mhz | 21" Monitor | Radeon 8500 | 1,5Gb SDRAM | SCSI

                Comment


                • #9
                  Yeah, I'm aware of the risks, but in my line of work they're often well outweighed by benefits. I specifically would NOT WANT to limit the scope of the array size define - I want it to be visible so that different codebases could include the same defines.

                  Trying to malloc an array to resize it wouldn't work. It's just a new array, somewhere else, with unknown default values. C++ arrays aren't resizable either that I'm aware of, unless you're using an STL class, and you'd probably want a vector anyway at that point.
                  Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                  Comment

                  Working...
                  X