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

stucash

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

}
Hatted Rooster

// 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i put a char pointer into an array char in a for loop in {C}

How to correctly typecast char array to struct pointer?

How do I return a pointer to something in the middle of a char array?

How do I cast /copy from a char array so that I may store it in a char pointer array?

How to convert a char array to a char pointer in C

How to copy char array to char pointer in C?

C - How to correctly free memory of a pointer to array of n char ((*p)[n])?

How to allocate char poiter to pointer char ** is it possible in C++ or do I need C for this

how can I make a copy the same value on char pointer(its point at) from char array in C?

returning a pointer that points to char array. How do i print out the entire contents of the array?

C *char pointer to char array

Char array pointer in C

How can I split a char pointer in C?

How do I access an index of a pointer inside of an array in C?

How do I use a double pointer to manipulate an array in C?

How do I read a C char array into a python bytearray with cython?

How do I receive a char array in a C function?

How do I convert an unsigned char array into a string in C?

How do I create an array of pointers of type char C++?

How do I initialize a char array with a memory address in C?

How do I change a particular int value in an array to a char in C?

How to initialize a char array from a char pointer in C++?

How do I get a char from a hex code stored in a character pointer in C?

C++ How to convert char pointer array to string array?

How to correctly work with array

How do i change the content of pointer array?

how do i pass an array with a pointer into a function?

How do I initialize a pointer to an Array of structure

How can I copy a string into a char array pointer?