473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

a question about pointers and string

hedonplay
#include<iostream>
using namespace std;

int main()
{

char *p;

p="i love c++";
cout<<p<<"\n";

return 0;
}

Display:i love c++
i am confusing about this kind of assignment. how is the string located? p is a pointer, the value of it should be an address.but why it displays as i love c++ .
if(p) ... why can p judged as true?
Apr 16 '07 #1
7 1358
About your first question, when you write char* p = "i am a pointer", what happens is that the string "i am a pointer" is allocated on the stack and the p pointer actually points to the memory address of the stack string, p itself is not allocated.

About your second question, if(p) means if the address p is pointing at is a valid one, meaning not NULL.
Apr 16 '07 #2
Savage
1,764 Expert 1GB
Take a look on this:

char a; // allocates space for a single char

char * b; // creates space for a pointer to a char ( or an array of char)

char c [SZ]; // similar to b, must have a constant integer substitued for SZ
// however, you also allocate space for the char, c is an address like b.

char * d [SZ]; // this is interesting... like c above, its a pointer... but with a twist

char ** e; // is the eqivalent of d above... and its called a pointer to a pointer.

So if you just cout b it will be the same as cout c if they have same string

Savage
Apr 16 '07 #3
#include<iostream>
using namespace std;

int main()
{

char *p;

p="";
if(p) cout<<"it is the address that p points is valid" <<"\n";
else cout<<"the address is null"<<"\n";
return 0;
}
Display:it is the address that p points is valid


can i understand like this?
Apr 16 '07 #4
JosAH
11,448 Expert 8TB
About your first question, when you write char* p = "i am a pointer", what happens is that the string "i am a pointer" is allocated on the stack and the p pointer actually points to the memory address of the stack string
That is most certainly not true, i.e. the literal string is not allocated on the stack.
The compiler has to be at its smartest to be able to deduce that string literals
can be allocated on stacks, especially when the address of that string might
be propagated to outer scopes.

The only thing that is guaranteed is that the string literal is allocated in memory
that is most likely not writable. Of course pointer p points to that string.

Most compilers put literal strings in the 'rodata' (Read Only data) segment,
some simply stick it in the data segments (where the string will be writable).
It is not defined by any language standard though so it's safest to think of
literal strings to be read only data.

kind regards,

Jos
Apr 16 '07 #5
JosAH
11,448 Expert 8TB
#include<iostream>
using namespace std;

int main()
{

char *p;

p="";
if(p) cout<<"it is the address that p points is valid" <<"\n";
else cout<<"the address is null"<<"\n";
return 0;
}
Display:it is the address that p points is valid


can i understand like this?
I don't understand it; change the line p= "" to this:
Expand|Select|Wrap|Line Numbers
  1. p= (char*)rand();
and see what is printed. Being non-null doesn't imply it's a valid address.

kind regards,

Jos
Apr 16 '07 #6
JosAH
11,448 Expert 8TB
Allow me to clarify on my previous reply: when a pointer has a non-null value
it is no guarantee that it's value would be valid. If a pointer does have a null
value you can be sure that it isn't pointing to anything. So basically you should
always initialize/assign your pointers to either null, indicating that it isn't
pointing to anything, or to a valid region in memory. Non initialized/assigned
pointers are a nono; always.

kind regards,

Jos
Apr 16 '07 #7
Banfa
9,065 Expert Mod 8TB
Non initialized/assigned pointers are a nono; always.
Or alternatively the source of many many hours of debugging :D
Apr 16 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
21
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
23
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers...
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.