help with dynamic arrays

This is a discussion on help with dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to create a dynamic array to display information in a structure....now keep in mind I'm fairly ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    help with dynamic arrays

    Hi everyone,
    I'm trying to create a dynamic array to display information in a structure....now keep in mind I'm fairly new to C++. Here's what I have:
    struct CandyBar
    {
    char brand[20];
    float weight;
    int calories;
    };

    int main()
    {
    CandyBar* p_candy = new CandyBar[3];
    p_candy[0] = {"Chunky", 3.5, 200};
    p_candy[1] = {"Monkey", 3.4, 100};
    p_candy[2] = {"Choco", 3.3., 50};
    ------------------------------------------------------------
    Now, this doesn't work obviously, and I'm curious how i can create a dynamic array to access structure members. I'm under the impression that to create a dynamic array, you need the type, and the pointer set to a memory location of the type...so whatever I'm doing wrong I just don't understand.. Any help would be great. thanks -Chap PS tell me if you need any more info

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    well, you could do:

    Code:
    strcpy(p_candy[0].brand, "Chunky");
    p_candy[0].weight = 3.5;
    p_candy[0].calories = 200;
    ...or just use an std::string for the name like:

    Code:
    struct CandyBar
    {
    string brand;
    float weight;
    int calories;
    };
    
    p_candy[0].brand = "Chunky";
    p_candy[0].weight = 3.5;
    p_candy[0].calories = 200;
    or give the structure a member function to set the values:

    Code:
    struct CandyBar
    {
     CandyBar(const char * _brand = "", float _weight = 0, int calories = 0) 
     {
      make(_brand, _weight, _calories);
     }
     CandyBar & make(const char * _brand, float _weight, int calories)
     {
      brand = _brand;
      weight = _weight;
      calories = _calories;
      return *this;
     }
     CandyBar & operator = (const CandyBar & cb)
     {
      return make(cb.brand, cb.weight, cb.calories);
     }
    string brand;
    float weight;
    int calories;
    };
    
    
    int main()
    {
     CandyBar candy("Chunky", 6.0, 1200);
     CandyBar* p_candy = new CandyBar[3];
     p_candy[0] = candy;
     p_candy[1].make("Monkey", 3.4, 100);
     p_candy[2].make("Choco", 3.3., 50);
     delete [] p_candy;
     return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    One word: Constructors
    Code:
    #include <cstring>
    #include <iostream>
    
    struct CandyBar
    {
      char brand[20];
      float weight;
      int calories;
    
      CandyBar()
      {
        brand[0] = '\0';
        weight = 0;
        calories = 0;
      }
    
      CandyBar ( char name[], float wt, int cal )
      {
        std::strcpy ( brand, name );
        weight = wt;
        calories = cal;
      }
    };
    
    int main()
    {
      CandyBar* p_candy = new CandyBar[3];
    
      p_candy[0] = CandyBar ( "Chunky", 3.5f, 200 );
      p_candy[1] = CandyBar ( "Monkey", 3.4f, 100 );
      p_candy[2] = CandyBar ( "Choco", 3.3f, 50 );
    
      for ( int i = 0; i < 3; i++ )
        std::cout<< p_candy[i].brand <<": "<< p_candy[i].weight <<" -- "<< p_candy[i].calories <<std::endl;
    }
    Of course, that just automates the task of assigning values. You could do away with them (despite their convenience) with relative ease:
    Code:
    #include <cstring>
    #include <iostream>
    
    struct CandyBar
    {
      char brand[20];
      float weight;
      int calories;
    };
    
    int main()
    {
      CandyBar* p_candy = new CandyBar[3];
    
      std::strcpy ( p_candy[0].brand, "Chunky" );
      p_candy[0].weight = 3.5f;
      p_candy[0].calories = 200;
    
      std::strcpy ( p_candy[1].brand, "Monkey" );
      p_candy[1].weight = 3.4f;
      p_candy[1].calories = 100;
    
      std::strcpy ( p_candy[2].brand, "Choco" );
      p_candy[2].weight = 3.3f;
      p_candy[2].calories = 50;
    
      for ( int i = 0; i < 3; i++ )
        std::cout<< p_candy[i].brand <<": "<< p_candy[i].weight <<" -- "<< p_candy[i].calories <<std::endl;
    }
    >char brand[20];
    The array for a brand name is a little disconcerting. C-style strings are error prone and difficult to work with. You would be much happier using C++ strings:
    Code:
    #include <iostream>
    #include <string>
    
    struct CandyBar
    {
      std::string brand;
      float weight;
      int calories;
    };
    
    int main()
    {
      CandyBar* p_candy = new CandyBar[3];
    
      p_candy[0].brand = "Chunky";
      p_candy[0].weight = 3.5f;
      p_candy[0].calories = 200;
    
      p_candy[1].brand ="Monkey";
      p_candy[1].weight = 3.4f;
      p_candy[1].calories = 100;
    
      p_candy[2].brand = "Choco";
      p_candy[2].weight = 3.3f;
      p_candy[2].calories = 50;
    
      for ( int i = 0; i < 3; i++ )
        std::cout<< p_candy[i].brand <<": "<< p_candy[i].weight <<" -- "<< p_candy[i].calories <<std::endl;
    }
    >float weight;
    This is fine if you understand that C++ defaults to double just about everywhere, this includes literal values, so you'll need to postfix them with f as in this code:
    Code:
    p_candy[0].weight = 3.5f; // The f in 3.5f forces 3.5 to be float
    >or give the structure a member function to set the values: <snip intermediate level code>
    now keep in mind I'm fairly new to C++.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    thanks...one more ;-P

    thanks for all the great info! It helped a lot and I really appreciate it. One quick question, what exactly does "strcpy" do in the line: strcpy(p_candy[0].brand, "Chunky");

    Once again thanks for all the help! -Chap

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what exactly does "strcpy" do in the line: strcpy(p_candy[0].brand, "Chunky");
    The biggest problem with C-style strings is that you cannot assign them. So the following will not work:
    Code:
    int main()
    {
      char buffer[10];
    
      buffer = "!testing!";
    }
    It begins to get scary when you use dynamic arrays rather than static arrays because assignment will compile but give you odd results:
    Code:
    int main()
    {
      char *buffer = new char[10];
    
      buffer = "!testing!";
    }
    The above code compiles and runs, but is a memory leak and a bug waiting to happen. The assignment does not copy the string into the dynamic array, and that confuses a lot of people. So a function was added to the standard library that allows us to pass two strings: the string to assign to and the string to assign. You can read strcpy as the assignment operator for C-style strings.
    Code:
    #include <cstring>
    
    int main()
    {
      char buffer[10];
    
      strcpy ( buffer, "!testing!" );
    }
    Now the code works like you want. A copy of the string "!testing!" is placed in buffer.

    As explained earlier, you can avoid all of this by using std::string rather than arrays wherever possible.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    awesome! Thank you so much! -Chap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21