Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-28-2005, 09:11 AM   #1
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
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()) {

	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.

Old 01-28-2005, 09:24 AM   #2
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Original Poster
Rep: Reputation: 30
also, is there a way to get the length of an array? in java there array_name.length. anything similar in c/c++?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 12:43 PM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 11:00 PM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 06:13 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 11:47 AM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration