user466534
user466534

Reputation:

Different behavior with different types of arrays

i have tested two different varianat of similary code ,suppose i have char array

char x[]="snow comes in winter ";

then following code

#include <iostream>
#include <string.h>
using namespace std;
int main(){

char x[]="snow comes in winter ";
int k=0;
int n=3;
cout<<x+n-k+1<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

prints "comes in winter " while following

#include <iostream>
#include <string.h>
using namespace std;
int main(){

//char x[]="snow comes in winter ";
int a[]={12,3,2,4,5,6,7};
int k=0;
int n=3;
cout<<a+n-k+1<<endl;



return 0;
}

prints 0xbffd293c if we change it a bit

#include <iostream>
#include <string.h>
using namespace std;
int main(){

//char x[]="snow comes in winter ";
int a[]={12,3,2,4,5,6,7};
int k=0;
int n=3;
cout<<*(a+n-k+1)<<endl;



return 0;
}

prints number 5. so my question is why we can access in case of char array so easily?what is main reason of it?i am very curiousy and please could anybody explain me?

Upvotes: 0

Views: 104

Answers (4)

jpalecek
jpalecek

Reputation: 47762

why we can access in case of char array so easily?

Because there's a convention that C strings are represented by char*, people assume char* are actually strings. The iostreams follow that and overload output for char* to print the string, whereas for an int* (in your second example), they print the representation of the address.

BTW there is a similar overload for input, which is most unfortunate, since you can't control that to prevent buffer overflows. ie. don't do this:

char x[10];
std::cin >> x;

Upvotes: 9

David Hammen
David Hammen

Reputation: 33116

Given a variable foo of type Foo*, the expression foo+n is also of type Foo* (assuming n is an integral type).

The default std::ostream mechanism for printing a pointer is to print the pointer. There is an override specifically for const char* pointers, which is to print the (presumably) null terminated string pointed to by that pointer.

Bottom line: Pointers that are not char* will be printed as pointers. Pointers that are char* will be printed as a string.

Upvotes: 1

MGZero
MGZero

Reputation: 5963

The left shift operator for cout is overloaded to handle char pointers. Meaning, when you give it a pointer to a char, it dereferences the pointer for you and so you get the data pointed to. This does not happen in the case of an int because there is no overload, and so you must dereference the pointer yourself.

Upvotes: 1

NPE
NPE

Reputation: 500357

The reasons for this go back to C.

In C, char* is typically used to represent a NUL-terminated string (as opposed to a pointer to an individual char). Therefore, functions taking char* typically treat the argument as a string. In this case the function is operator<<, and what it does is print the argument out as a string.

No such thing can be said about int*, which is just that, a pointer to an int. Here, operator<< prints out the value of the pointer (i.e. the address). It's only when you explicitly dereference the pointer that the pointed-to value gets printed.

Upvotes: 2

Related Questions