dynamic arrays and structures

This is a discussion on dynamic arrays and structures within the C++ Programming forums, part of the General Programming Boards category; I'm trying to set up a structure within a structure, but there's also two dynamic arrays inside: Code: struct Sentence ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    dynamic arrays and structures

    I'm trying to set up a structure within a structure, but there's also two dynamic arrays inside:

    Code:
    struct Sentence {
    int numwords; 
    char **words;
    };
    
    struct Document {
    int numLines;
    Sentence *sentences;
    } Doc;
    that's a structure (Doc) with a dynamic structure inside (Sentence *sentences) with a dynamic array inside that (char **words)

    The problem is I can't seem to access the char **words portion of the array.

    I've set up:
    Code:
    int len=0;
    Doc.sentences = new Sentence[len];
    However, I can't seem to be able to figure how to dynamically allocate memory for **words so that it becomes an array of pointers.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    Code:
      char **strings;
      
      strings = new char*[15];
      
      int i;
      
        for(i = 0; i < 15; i++) {
          strings[i] = new char[100];
      
          sprintf(strings[i], "%i\n", i+1);
      
          printf("%s", strings[i]);
         }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  2. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 10:48 PM
  3. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 05:29 PM
  4. Concerning Dynamic Arrays
    By CeeCee in forum C Programming
    Replies: 15
    Last Post: 11-25-2001, 01:19 PM

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