stucash
stucash

Reputation: 1258

How do I correctly work with char pointer to array in C++?

I am trying to pick up my C++; I have basic understanding of pointers and references; but when it comes to char pointer to array, it seems nothing works for me.

I have a small piece of codes here (omitted include and namespace statements), I have included my questions as comments below:

I have gone through at least 5 other questions on SO to try to understand it; but those answers didn't the answer I expected and to the extent that could help understand the actual issue there.

Could you kindly explain the problems I commented below with a bit of depth from the surface (so please don't dive into it directly)?

int main(){

    // 1 this is a char pointer to a char;
    char * c = new char;
    *c ='A';
    cout << c << endl; // this gives me memory address;
    cout << *c << endl;// this gives me the value in the memory address;


    // 2 this is a char array initialised to value "world";
    char d[6] = "world";
    cout << d[0] << endl; // this gives me the first element of char array;

    // 3 this is char pointer to char array (or array of char pointers)?
    char * str = new char[6];
    for(int i=0;i<6;i++){ // 
        str[i]=d[i];     // are we assigning the memory address (not value) of respective elements here? 
    }                   // can I just do: *str = "world"; what's the difference between initialising with value
                       // and declaring the pointer and then assign value?  

    char * strr = "morning";

    char b[6] = "hello";


    cout << b << endl;
    cout << (*str)[i] << endl; // why? error: subscripts requires array or pointer type
    cout << str[1] << endl;
    cout << (*strr)[1] << endl; // why? error: subscripts requires array or pointer type

}

Upvotes: 0

Views: 619

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36463

// 1 this is a char pointer to a char;

Right.

// 2 this is a char array initialised to value "world";

Right, "world\0" is created by the compiler and is put in the read-only memory area of the program. Note that this is called a string literal. Then the string is copied over to the char array d.

// 3 this is char pointer to char array (or array of char pointers)?

That's a char pointer yes, a pointer to a single char.

// are we assigning the memory address (not value) of respective elements here?

No, you're assigning the values of the elements. This is allowed because str[i] is the same as *(str + i) so you can use the same "array style" access with the pointer str. You're looping over the individual chars you have allocated with new and are assigning them the value of the chars in the char array d.

// why? error: subscripts requires array or pointer type

Because you already dereference str (which is pointing at the start of the 6 element char array) with * which gives you a char, then you try to use that char like an array with [1] which makes no sense. *str would give you 'w' (the first element). And str[1] would give you *(str + 1) which is 'o' (the second element), don't double up.


A small-big side note, string literals are of type const char[], not char[], they're placed in read only memory and thus they can not be altered by the program (don't write to them).

char * strr = "morning";

This is very very bad, it treats a const char[] as a char[], this has been deprecated in the standard for a while now and according to the current standard this is even illegal, yet compilers still allow it for some reason.

Because compilers allow this you could get some nasty situations like trying to modify the string literal:

char * strr = "morning";
strr[0] = 'w'; // change to "worning"

This will attempt to write to read-only memory, which is undefined behaviour and will probably/hopefully get you a segmentation fault. Long story short, use the appropriate type to have the compiler stop you before the code reaches runtime:

const char * strr = "morning";

side side note : don't forget to delete anything you allocated with new.

Upvotes: 5

Related Questions