char pointer again

Hello everybody,

I have a problem with the following function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// get a column from a string for a given delimiter
char * getColumn(char * inputStr, int columnNo, char delimiter)
{
	// define variables
	char inputStr_aux[_COLUMN_SIZE];
	strcpy(inputStr_aux, inputStr);
	char * columnBegin = inputStr_aux;
	char * columnEnd = strchr(inputStr_aux, delimiter) + 1;
	int columnPos = 1;

	// get column
		// only one column and input string is not terminated with a delimiter
		if (columnEnd == NULL && columnNo == 1)
			return columnBegin;
		// more than one column and no delimiter
		else if (columnEnd == NULL && columnNo > 1)
		{
			writeTo(_LOG_FILE, "getColumn(char * inputStr, ...) - invalid column number!");
			myPause();
			return columnBegin;
		}
		// other cases
		else
		{
			while (columnPos < columnNo)
			{
				columnBegin = strchr(columnBegin, delimiter) + 1;
				columnEnd = strchr(columnBegin, delimiter) + 1;
				columnPos++;
			}

			// return corresponding column
			columnBegin[columnEnd - columnBegin - 1] = '\0';
			cout << columnBegin << endl;
			return columnBegin;
		}
}


I call it with the following piece of code

1
2
char line[] = "car;train;plane;boat;;bike;spaceship";
char * column =	getColumn(line, 3, ';');


and get an assertion error. The problem seems to be with the last return columnBegin. The line above cout << columnBegin << endl; returns a correct answer... Could you please advice me where the mistake might be?

Thanks a lot in advance,

Michal
Last edited on
You cannot return a pointer to a local variable like 'inputStr_aux'. Local variables won't exist anymore after the function returns
Thanks for help. Here is a modified function code (just in case someone is solving the same problem).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// get a column from a string for a given delimiter
void getColumn(char * inputStr, int columnNo, char delimiter, char * outputStr)
{
	// define variables
	char inputStr_aux[_COLUMN_SIZE];
	strcpy(inputStr_aux, inputStr);
	char * columnBegin = inputStr_aux;
	char * columnEnd = inputStr_aux;
	int columnPos = 1;

	// get column
		// only once column that is not terminated by a delimiter
		if (columnNo == 1 && strchr(columnBegin, delimiter) != NULL)
			columnEnd = strchr(columnBegin, delimiter) + 1;
		// columnNo smaller than 1
		else if (columnNo <= 0)
		{
			writeTo(_LOG_FILE, "getColumn(char * inputStr, ...) - number of column has to be bigger than 0!");
			columnEnd = strchr(columnBegin, delimiter) + 1;
			myPause();
		}
		// other cases
		else
		{
			while (columnPos < columnNo)
			{
				if (strchr(columnBegin, delimiter) != NULL)
				{
					columnBegin = strchr(columnBegin, delimiter) + 1;
					columnEnd = strchr(columnBegin, delimiter) + 1;
					columnPos++;
				}
				// number of delimiters is smaller than columnNo
				else
				{
					writeTo(_LOG_FILE, "getColumn(char * inputStr, ...) - number of columns does to correspond to number of delimiters!");
					columnEnd = columnBegin; //return the last column
					myPause();
					break;
				}
			}
		}

	// return the corresponding column
	if (columnBegin != columnEnd)
		columnBegin[columnEnd - columnBegin - 1] = '\0';

	strcpy(outputStr, columnBegin);
} 


I call the function through

1
2
3
4
5
	char * line = new char[_COLUMN_SIZE];
	line = "car;train;plane;boat;;bike;spaceship";
	char * column = new char[_COLUMN_SIZE];
	getColumn(line, 2, ';', column);
	cout << column << endl;

which returns

 
train


However I have another question. If I call the function using

1
2
3
4
5
6
7
8
	char * line = new char[_COLUMN_SIZE];
	line = "car;train;plane;boat;;bike;spaceship";
	char * column = new char[_COLUMN_SIZE];
	getColumn(line, 2, ';', column);
	cout << column << endl;

	delete [] line;
	delete [] column;

I get an error. Why? What is wrong on using delete?

Thanks a lot,

Michal
Last edited on
It's unnecessary to use new with an array that has a constant size. Writing it so char * line[_COLUMN_SIZE]; is perfectly ok.

I get an error. Why? What is wrong on using delete?
If you get an error, tell what the error is. In that case on line 2 you reset the dynamic memory with a static one (which you cannot delete and is const!)



Using const improves both readability and safety:
1
2
3
4
5
6
const char *line = "car;train;plane;boat;;bike;spaceship"; //"..." is always a constant string and will crash if written to
...
void getColumn(const char * inputStr, int columnNo, char delimiter, char * outputStr) // 'const char * inputStr' means that the function cannot modify the input string
{
...
}
OK I see the point. Thanks a lot. Michal
Topic archived. No new replies allowed.