hey guys. just wondering what it means when a function has this in it....

void add(string title, string authors[], int nAuthors);

the [] next to authors. what does this mean?

10 Years
Discussion Span
Last Post by dan_e6

doh....im an idiot....
ive been up all night doing other things and ive never really had to use a string array so i saw it and was like huh.
that's really nifty :-) thanks!


I tend to just use arrays of characters as strings....I find them more versatile (and have recently been programming in C, so...)


well if i have a string array like

string authors[]

inside a structure named library and i have a pointer to the library called temp. how do i make the array of authors bigger? it doesnt like me doing this....

struct library
string authors[];

library *temp;
int nAuthors = 3;
temp->authors = new string[nAuthors];


> how do i make the array of authors bigger?
If the size is going to be dynamic, you'll have a more pleasant time using vectors than arrays:

#include <string>
#include <vector>

struct library {
  std::vector<std::string> authors;

Dynamically resizing arrays is tedious and error prone.


I suggest you take a look at vectors if you want to change array-sizes on the fly.

for example:

#include <vector>
#include <string>

string str = "something";
vector <string> array;

[edit] double post with Edward


i know all about vectors and i love them. but we're not allowed to use anything from STL and that includes vectors if im not mistaken :(


You could always try to implement vectors (or alternatively, lists) yourself...using structs and pointers...its actually probably a good exercise, but you may not be allowed to do this either.


hmmm well i got it working.

ive basically got a linked list that contains the book name and the different authors (there can be n authors for each book).

struct node
           string bookTitle;
           string *authors;
           node *next;
          node *start_ptr;

void Library::add(string title, string authors[], int nAuthors)
     node *temp = new node;
     temp->bookTitle = title;
     temp->authors = new string[nAuthors];
     for (int i = 0; i<nAuthors; ++i)
         temp->authors[i] = authors[i];
     temp->next = NULL;
         if (start_ptr == NULL)
        start_ptr = temp;
        node *p = start_ptr;
        while (p->next != NULL)
            p = p -> next;

        p->next = temp;

and this works as far as i can tell.

edit: how would i figure out how many authors are stored in any given node??


> we're not allowed to use anything from STL
Here's a utility function that will resize an array of strings:

#include <iostream>
#include <string>

namespace Ed {
  void resize(std::string*& list, int oldSize, int newSize)
    // Don't do anything if the sizes are the same
    if (newSize == oldSize)

    std::string *result = new std::string[newSize];
    int nCopied = oldSize;
    // Don't copy everything if the array got smaller
    if (newSize < oldSize)
      nCopied = newSize;

    // Preserve existing stored strings
    for (int i = 0; i < nCopied; i++)
      result[i] = list[i];

    delete[] list;
    list = result;

> how would i figure out how many authors are stored in any given node??
Store it as a separate member in the node. Or better yet, write an Authors collection class that stored all of the information you need and handles the resizing of the array internally. :)


thanku :) i just figured out about storing it as a seperate member lol. im half asleep.
i just wrote a for loop without any ";" in it and tried to compile lol oops. tells how tired i am. im hittin the hay. thanks for all the help guys

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.