10

In C++, I have a char array defined as:

char miniAlphabet[] = {'A','B','C','D','E', '\0'};

I want to modify the values of this array from other functions without passing it to those functions. This is where you use pointers right?

So my question is what is the correct way to make a pointer to this char array. Would I just make a pointer to the first element and then when I want to modify the values I step through memory until I hit the end character?

1
  • Just looked this up, yes a lot better than \0. Thanks Commented Mar 12, 2012 at 16:08

4 Answers 4

22
char miniAlphabet[] = {'A','B','C','D','E', '\0'};

These are equivalent.

char *p1 = miniAlphabet;

char *p2 = &(miniAlphabet[0]);

Note that the () are useless for the operators' precedence, so

char *p3 = &miniAlphabet[0];

In C/C++ the name of an array is a pointer to the first element of the array.

You can then use pointers' math to do some magic...

This will point to the second element:

char *p4 = &miniAlphabet[0] + 1;

like

char *p5 = &miniAlphabet[1];

or

char *p6 = miniAlphabet + 1;
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I have a quick question. when you do char *p1 = miniAlphabet, p1 now stores the address of the first element of the array. How come, when we do cout << p1 << endl, we get the entire char array (in this case ABCDE) and not the address of the first element?
@DannyBrown Because the creator of cout decided so. It could even have always written "hello world" for any char* pointer, but it would have been stupid. The most intelligent thing to do if you are in the profession of writing text, is to print a char* as a string. If you want to print a pointer address, do a << static_cast<void*>(p1)
Considering that in C++ the name of an array is a pointer to the first element of the array, is is correct to say that if "char miniAlphabet[] = {'A','B','C','D','E', '\0'};" then in "char *p1 = miniAlphabet;" "*p1" is a pointer to a pointer?
@DanielZardo It is the opposite. With *p1 you are dereferencing the pointer (accessing the value pointed by the pointer), so you are obtaining the first element. char p2 = *p1;. A pointer to a pointer would be char **p3 = &p1;
@theMyth The pointer points at the first character, but it doesn't include a "length". A char* is both a pointer to a single char AND a pointer to what is called a C string (a zero terminated string). It depends on how you use it.
|
2

The position of the array in memory is the same as the position of its first element. So for example, both function signatures below are equivalent:

function changeArray(char[] myArray);
function changeArray(char* myArray);

And you can call either version like this:

changeArray(miniAlphabet);

...or

changeArray(&(miniAlphabet[0]));

2 Comments

I was under the impression that a char* is a constant on declaration. This being the case, wouldn't the second function not be able to change anything about the char array, while the first can?
You can't change the value of a char* inside the function, but you can change the value it points to, in this case, the array. So both declarations allow changes to the original array.
1

If you pass an array to a function, you ARE passing by reference as an array variable is really just a pointer to the address of the first element in the array. So you can call a function like this:

funcName(miniAlphabet);

and receive the array "by reference" like this:

void funcName(char *miniAlphabet) {
   ...
}

Comments

1

In short, yes. When you create the array miniAlphabet is a pointer to the array of the size and type you defined. Which is also the pointer to the first element.

An example of what you are suggesting.

void foo(char *array)
{
    array[0] = 'z';
    array[2] = 'z';
}

...

char myArray[] = { 'a', 'b', 'c'};
foo(myArray);
//myArray now equals  {'z', 'b', 'z'}

also of note, dereferencing myArray gives you the value in the first element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.