473,614 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does this pointer work?

this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];
May 29 '06 #1
14 2417
"jagguy" <jo**********@o ptusnet.com.au> wrote in message
news:44******** **************@ news.optusnet.c om.au...
this works

char *p ;

p="xat";
"xat" is a constant char array and p now points to it.
cout << p <<endl;

i thought you need something like this

char *p=new char[10];

May 29 '06 #2

"jagguy" <jo**********@o ptusnet.com.au> wrote in message
news:44******** **************@ news.optusnet.c om.au...
this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];


Sure it works. It equilant to this code

const char *str = "xat";
char *p = (char *)str;
cout << p << endl;

When the compiler generates the assembly
"xat" goes into the data segment as a const.with some address
when you do p = "xat" the compiler just set the address pointed by p to the
address of "xat" in the data segment.

//eric
May 29 '06 #3

char *p ;

p="xbbat";

cout << p <<endl;


i thought you need something like this

char *p=new char[10];


Sure it works. It equilant to this code

const char *str = "xat";
char *p = (char *)str;
cout << p << endl;

When the compiler generates the assembly
"xat" goes into the data segment as a const.with some address
when you do p = "xat" the compiler just set the address pointed by p to
the address of "xat" in the data segment.

//eric

if it is a constant then this shouldn't work but it odes

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;
May 29 '06 #4

"Eric Jensen" <er**@no.spam.c om> wrote in message
news:44******** *************** @dread16.news.t ele.dk...

"jagguy" <jo**********@o ptusnet.com.au> wrote in message
news:44******** **************@ news.optusnet.c om.au...
this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];


Sure it works. It equilant to this code

const char *str = "xat";
char *p = (char *)str;
cout << p << endl;

When the compiler generates the assembly
"xat" goes into the data segment as a const.with some address
when you do p = "xat" the compiler just set the address pointed by p to
the address of "xat" in the data segment.

//eric


if it is a constant then this shouldn't work but it odes

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;
May 29 '06 #5

"jagguy" <jo**********@o ptusnet.com.au> wrote in message
news:44******** *************** @news.optusnet. com.au...
if it is a constant then this shouldn't work but it odes

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;


char *p; // <- this is just a pointer. it points to a location in memory

"xbbat" is a string literal, located inside the binary (.exe).
When you run your program it's loaded into memory.
you assignments will only set the value of the pointer p to point at the
area in memory that contains the string "xbbat" (wich is loaded into memory
with the program when executed).

char *p;
p = "string";

You do not assign the value "string" to p.
You do only set the value of p to the address in memory where the first char
of "string" is located.

However, you should not do what your doing.

It would make more sense to write:
const char *p = "sometext";
std::cout << p << std::endl;

witch is basicly the same as what you do.

//eric
May 29 '06 #6
jagguy wrote:
this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];


An analogue to your code is:

int a = 100;
int p = &a;

cout << *p;

The pointer points to a valid memory, it should work.

Ben
May 29 '06 #7
int a = 100;
int p = &a;

cout << *p;

int *p = &a;
(Although I presume it was nothing more than a typo)
-Tomás
May 29 '06 #8
jagguy posted:
this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];

You posted two separate threads, and now the replies are all over the
place.

I left you a reply in the other thread.
-Tomás
May 29 '06 #9

"jagguy" <jo**********@o ptusnet.com.au> skrev i meddelandet
news:44******** *************** @news.optusnet. com.au...

if it is a constant then this shouldn't work but it odes

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;


It is a special rule for string literals. You can assign their address
(a const pointer) to a non-const char pointer.

Originally, the C language didn't have const, so

char* p = "Hello";

has always worked this way.

Later when C++ made string literals constants, this special rule was
added to support old C code. It doesn't work for any other kind of
pointers.
Bo Persson

May 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1770
by: PKH | last post by:
This is a part of an autopointer template i'm using. I originally had a Get() member that returned the pointer, but thought it would nice to be able to use the -> operator. The question is why does the following line work, and call cAP.m_pcPointer->Testing() ? cAP->Testing(); // ??? cAP-> returns a CTest*, so it looks to me like the line reads cAP.m_pcPointer <whitespace> Testing(), but it actually does
3
1449
by: Zheng Da | last post by:
Will the following class work well? If it can not work correctly in some situation, could you help me to fix it? Thank you. //the class will work like the reference in java //when you create a object, you must save the pointer of the object in super_auto_ptr //the class cannot work correctly in multithread
24
3795
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
5
1329
by: yogesh | last post by:
Why does the following code gives an error. char c="sometext"; char **cp = &c; This gives the error "can not covert from char (*) to char**; But when i change the code to char *c="sometext";
5
8076
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
7
1502
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char is_reference_helper1(...); template <class Tno_type is_reference_helper2(T&(*)(wrap<T>)); yes_type is_reference_helper2(...);
5
1690
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. My observation: 1. Compiler does not complain.
42
2128
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone give any advise here? -------------------------------------------------- #include <stdlib.h> #include <stdio.h> int main(void) {
8
1632
by: mdh | last post by:
May I ask this. Given the declaration: int myf( int, int); and a function pointer: (*fp)=int myf(int, int); where I am initializing fp to point at myf....or trying to..
19
4181
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port); pTemp->Connect(); m_collClients.push_back(pTemp);
0
8142
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,...
0
8642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8294
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,...
0
8444
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
agi2029
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...
1
6093
isladogs
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...
0
5549
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();...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.