I am a beginner and have some confusion with respect to pointers and
strings. It seems that the pointers with dealing with integer arrays
behave differently, as opposed to strings. Can some one explain me the
difference?
Sample Program:
int main()
{
int array[]={1,2,3,4,5};
char array1[]={"Name is Max"};
int *ptr;
char *ptr1;
ptr=array;
ptr1=array1;
cout<<"The array is "<<array<<e ndl;
cout<<"ptr is "<<ptr<<end l;
cout<<"*ptr is "<<*ptr<<en dl;
cout<<"The array is "<<array1<<endl ;
cout<<"ptr1 is "<<ptr1<<en dl;
cout<<"*ptr1 is "<<*ptr1<<e ndl;
}
Output:
The array is 0xbfe09380
ptr is 0xbfe09380
*ptr is 1
The array is Name is Max
ptr1 is Name is Max
*ptr1 is N
I can understand that in both the cases, *ptrx points to the element
in it's address location. But why in the case of "ptr1", when I am
using a string ptr1 refers to "Name is Max" and not the address of the
"array1', like it did with "array".
Thanks
Manny 1 1617
Slain wrote:
I am a beginner and have some confusion with respect to pointers and
strings. It seems that the pointers with dealing with integer arrays
behave differently, as opposed to strings. Can some one explain me the
difference?
Sample Program:
int main()
{
int array[]={1,2,3,4,5};
char array1[]={"Name is Max"};
int *ptr;
char *ptr1;
ptr=array;
ptr1=array1;
cout<<"The array is "<<array<<e ndl;
array decays into a pointer to its first value. As cout has a way to output
const void* (== void const*), but not int*, the pointer is implicitly
converted to const void*.
cout<<"ptr is "<<ptr<<end l;
Here, you already have an int*. It is converted to a const void* and
printed.
cout<<"*ptr is "<<*ptr<<en dl;
*ptr is an int, and cout has a << operator to print ints.
cout<<"The array is "<<array1<<endl ;
As with array, array1 also decays to a pointer to its first element. But
this time, the type is char*, and that can convert to a const char* (==
char const*), for which cout has a special output operator: It is treated
as a null terminated string.
cout<<"ptr1 is "<<ptr1<<en dl;
Will call the same operator as array1 did.
cout<<"*ptr1 is "<<*ptr1<<e ndl;
And this uses the char output operator.
}
Output:
The array is 0xbfe09380
ptr is 0xbfe09380
*ptr is 1
The array is Name is Max
ptr1 is Name is Max
*ptr1 is N
I can understand that in both the cases, *ptrx points to the element
in it's address location. But why in the case of "ptr1", when I am
using a string ptr1 refers to "Name is Max" and not the address of the
"array1', like it did with "array".
Because the standard says const char* are to be treated specially by cout
and other ostream objects. In the same manner, wostream and wcout treat
const wchar_t* specially.
--
rbh This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: agent349 |
last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing...
|
by: Joe C |
last post by:
I'm a hobbiest, and made the forray into c++ from non-c type languages about
a year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very much
when dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char*...
|
by: maniac |
last post by:
Hey guys, I'm new here, just a simple question.
I'm learning to Program in C, and I was recommended a book called,
"Mastering C Pointers", just asking if any of you have read it,
and if it's worth the $25USD.
I'm just looking for a book on Pointers, because from what I've
read it's one of the toughest topics to understand.
thanks in advanced.
|
by: Deniz Bahar |
last post by:
Hello all,
Often times programs in C have arrays used as buffers and shared among
different sections of code. The need arises to have position
indicators to point to different parts of an array (example: point to
top of stack). Before I even got K&R2 I used to just define extra
pointers to types equal to the element type of the arrays to act as
indicators. Now flipping through K&R2, I see they use int variables to
act as "offsets." ...
|
by: raphfrk |
last post by:
I have the following code:
char buf;
printf("%lp\n", buf);
printf("%lp\n", &buf);
printf("%lp\n", buf);
printf("%lp\n", buf);
printf("%d\n", buf-buf);
| |
by: jagguy |
last post by:
can you declare variables as pointers without using new or make them point
at another variable.
this works I thought it shouldn't
char *p ;
p="xat";
cout << p <<endl;
|
by: arnuld |
last post by:
at the very beginning of the chapter, i see some statements i am
unable to understand. i know the "Pointer" takes the address of a
variable, useful if, in case, we want to manipulate that variable as
each Function gets its private copy of arguments:
char c;
char** ppc; // what is the use of "pointer to pointer"
int* a; // why it is necessary to have "an array if pointers"
|
by: Piotrek |
last post by:
Hi,
Like almost all of beginners I have problem understanding pointers.
Please, look at this piece of code, and please explain me why myswap
function doesn't work as it's supposed to do, whereas myswap2 is doing
exactly what I want it to do - swaping pointers. Where I made a mistake?
Thanks
void myswap(char *pa, char *pb){
char *tmp;
tmp=pa;
|
by: JOhn |
last post by:
can someone please post some complicated question on pointers??
moreover while reading pointers I found out that there is a lot of
difference between an arracy of intergers and an array of
characters(string) ............. relating to
pointers ................am i correct................the way pointers
behave when they have a char array as an address and the way they
behave when they have an integer array as an address ????
please...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |