-   Programming (
-   -   help with c++ code/pointers/arrays (

drisay 01-28-2005 08:11 AM

help with c++ code/pointers/arrays
hi all,

i'm attempting a first c++ app here. it's a music db thing. anyways what it is is doesn't really matter. i'm guessing that it is a pointer issue. i guess i just don't quite understand them yet. here's my code so far.


#include <string>
#include <fstream>
#include <iostream>
using namespace std;

#define SCALES_FILE "../etc/scales.m"
#define CHORDS_FILE "../etc/chords.m"

#define MAIN_DELIM "="
#define SEC_DELIM ","
#define COMMENT_CHAR 0x23

#define SCALE_LENGTH 3
#define CHORD_LENGTH 2

#define BUFFER_MAX 80

// error message values

typedef struct {
        char *name;
        int increments[1];
        int increment_length;
} scale;

typedef struct {
        char *name;
        char **indicators;
        int increments[1];
        int increment_length;
} chord;

scale *scales;
int scale_length;
chord *chords;
int chord_length;

int setGlobalVariables();
scale setNextScale(char *buffer);

int setGlobalVariables() {
        char buffer[BUFFER_MAX];

        // set the scales from the config files
        ifstream scalefile(SCALES_FILE);
        if (!scalefile.is_open()) {
                return SCALE_FILE_UNDEFINED;

        int index = 1;
        scale *tmp1;
        while (!scalefile.eof()) {
                scalefile.getline(buffer, BUFFER_MAX);

                // skip parsing the line if it is a comment (#) or a zero length line
                if (buffer[0] != (char)COMMENT_CHAR && strlen(buffer) > 0) {
                        tmp1 = new scale[index];

                        if (index > 1) {
                                for (int i = 0; i < (index - 1); i++) {
                                        tmp1[i] = scales[i];
                                        cout << tmp1[i].name << "tmp1" << endl;

                        tmp1[(index - 1)] = setNextScale(buffer);

                        delete scales;
                        scales = new scale[index];

                        for (int i = 0; i < index; i++) {
                                scales[i] = tmp1[i];
                                cout << scales[i].name << "scales" << endl;

                        delete tmp1;
        scale_length = index -1;


        cout << scales[0].name << endl; // result= Minor (*wanted result= Major)
        cout << scales[1].name << endl; // result= Minor (*wanted result= Minor)

        return 0;

scale setNextScale(char *buffer) {
        scale tmp;
        char *tmpscale; = strtok(buffer, MAIN_DELIM);
        tmpscale = strtok(NULL, MAIN_DELIM);

        // parsing for tmpscale required here

        return tmp;

here's my question... look at the section where i put comment // result=. i'm getting the wrong result and i'm assuming that it's a pointer/memory address issue that i don't quite get. every array entry takes the value of the last entry inserted.

also, is there an easier way to add extra memory to an array without completely reinitialising the entire thing?

any other comments on the code... please feel free to tear it apart... looking for any critics as i want to improve my coding.


drisay 01-28-2005 08:24 AM

also, is there a way to get the length of an array? in java there array_name.length. anything similar in c/c++?

All times are GMT -5. The time now is 11:11 PM.