-3

I am a beginner to c++. The below code snippet is taken from a program for parsing text input.

const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = ",";

I couldn't find a reason for why the programmer used const pointer for variable DELIMITER, as he didn't use const pointer for other variables. Please clarify.

user294664
  • 109
  • 12
  • 1
    Have you looked at the point where the constants are used? I suspect there is a function used somewhere that expects a string (to support several delimiter characters). – StoryTeller - Unslander Monica Oct 18 '16 at 07:14
  • 1
    For people not bothering to read this question properly: OP is asking why `const int` **no pointer** whereas `const char` **uses pointer**. – Disillusioned Oct 18 '16 at 07:18
  • I'm fairly sure this is a dup question. I just haven't found it. `const char*` is the 'old' way to represent a string. You'll notice that the value is also initialised as a string using double quotes instead of single quotes if it were a regular char: `const char c = ',';` This search should give you lots of reading material: [c++ const char*](http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+const+char*) – Disillusioned Oct 18 '16 at 07:26

1 Answers1

4

512 and 20 are constants of type int. Their values can be stored in objects (variables) of type int. There's no need for pointers.

A string literal like "," is not a simple scalar value. It's of type const char[2], an array of two chars. (const means read-only.)

Though arrays and pointers are very different things (and don't let anyone tell you otherwise!), arrays in C and C++ are most commonly manipulated via pointers to their elements. Thus a char* pointer can be used to, for example, traverse a string and provide access to each of its elements.

The extra const means that the pointer itself cannot be modified.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631