C++ dynamic memory allocation (char[] and int[])

808 Views Asked by At

str is a pointer, why not use str for input and output? Not *str.
p is a pointer, why use *p for input and output? Not p.

#include<iostream>
using namespace std;

int main()
{
   
    char* str = new char[20];
    cin>>str;
    cout<<str<<endl;
    delete []str;

    int* p = new int[3];
    cin>>*p;
    cout<<*p<<endl;
    delete []p;

    return 0;
}
5

There are 5 best solutions below

0
Lukas-T On BEST ANSWER

The operator overloads << and >> have special overloads for const char* and char* respectively, because those are null-terminated C-style strings. They are treated diifferently than other pointers/other arrays.

Here's a little comparison of the semantics used:

cin >> str;

means "read a null terminated string into an array, where str is the pointer to the first element".

cout << str; 

means "Print a null terminated string, where str is the pointer to the first element".


However there are such semantics for other pointer types like int*.

cin >> p;

wont work, there is no such thing as "reading an array of ints", or "reading a pointer", while

cin >> *p; 

works and means "read a single integer and store it in the value of p", that is, the first element in the array get's modified.

cout << p;

means "print the value of the pointer", again, because there are no special semantics for int* like "Print array of ints". On the other hand

cout << *p;

meanse "Print one integer, that is the value of p", that is, the first element in the array get's printed.

0
Thibault Cimic On

Cause a pointer is a variable who's value is an adress, the value of the adress pointed is then accessible by the * operator. example :

char *str = new char[20];

str is a pointer so it has :

_ an adress &str = adress1 _ a value str = adress2 (it's adress because it's a pointer)

And at this adress2 are 20 character on continuous adress, so first character would be in adress adress2, then next one adress2+1, then adress2+2 etc..

0
Swift - Friday Pie On

Assuming that there was a grammar mishap in bullet 1.

  1. str is a pointer to a char but in this case it point to first one in an array of such. *str is a reference to single char (byte). pointers can be used to pass string by passing address of array's beginning. We are guaranteed to be able to increment that pointer and dereference it in order to access following characters

E.g. this is how strlen may work:

  size_t strlen(char *p)
  {
    size_t len = 0;
    while (*p++ != 0) len ++;
    return len;
  }
  1. p is value of pointer to int, there is no meaning in writing to it. *p is reference to first element of array, of type int.
0
Jeffrey On
char* str = new char[20];
cin>>str;

You are passing the pointer to a series of char to cin because cin is expected to read multiple chars at the location pointed to by the pointer.

int* p = new int[3];
cin>>*p;

You are passing a single int, by reference, to cin, because cin is expected to read a single int. There is no read functionality that will read a sequence of ints, because a sequence of int is not an usual thing. However, a sequence of char is a c-string and a typical thing. That's why cin has an overload that reads to a char pointer.

0
Vlad from Moscow On

It is better to rewrite this code snippet

int* p = new int[3];
cin>>*p;
cout<<*p<<endl;
delete []p;

like

int* p = new int;
cin>>*p;
cout<<*p<<endl;
delete p;

because there is used only one object of the allocated array of three objects of the type int.

In the first case you are dealing with a character array that accepts or stores a string. For character arrays for example the operator >> is declared like

template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&,
charT*);

That is the operator needs to get access not only to the first character of the array but also to subsequent characters of the array using the pointer arithmetic. The operator does not change the passed pointer. It changes the objects of the type character pointed to by the pointer.

For objects of the type int the operator >> is declared like

basic_istream<charT,traits>& operator>>(int& n);

It is enough to pass a scalar object by reference to change it using the operator.