#include<iostream>
#include<string.h>
int main()
{
char* str="my name"; /*plz explain what is pointer-name,and what is string-
name here(realy confused) ? i am new to pointers :) */
int len=strlen(str);
char* ptr;
ptr=new char[len+1]; /*is this ptr pointer or string*/
strcpy(ptr,str);
cout<<ptr;
delete ptr; /*how this works */
cout<<endl<<ptr; /*this 'ptr' should be destroyed as effect of "delete ptr"
,but still displaying here when executed*/
return 0;
}
saransh60 0 Newbie Poster
Recommended Answers
Jump to Posthi
from what i can see ill explain whats going on
#include<iostream> #include<string.h> int main() { char* str="my name"; //char* means pointer so this creates a pointer to a character array called str. //str is initilised upon creation to the string "my name". As far as …
Jump to Post@mike ,... Ahh i see that was never explained to me and i found out the hard way cleaning up an array of classes untill i used delete[] so i thought it was how it had to be done. thanks
Jump to Post@Kanoisa (saransh, please ignore):
>> //this shouldent work it should be delete [] ptr.
For primitive types like char, it works fine. When dealing with objects with non-trivial destructors, if delete[] is not called it will not call the individual destructor of each element, but the overall memory is still …
Jump to Post#include<iostream> #include<string.h> int main() { char* str="my name"; /*plz explain what is pointer-name,and what is string- name here(realy confused) ? i am new to pointers */ int len=strlen(str); char* ptr; ptr=new char[len+1]; /*is this ptr pointer or string*/ strcpy(ptr,str); cout<<ptr; delete ptr; /*how this works */ cout<<endl<<ptr; …
All 13 Replies
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
Kanoisa 52 Posting Whiz in Training
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
Kanoisa 52 Posting Whiz in Training
Fbody 682 Posting Maven Featured Poster
saransh60 0 Newbie Poster
Aranarth 126 Posting Whiz in Training
mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster
saransh60 0 Newbie Poster
Aranarth 126 Posting Whiz in Training
Fbody 682 Posting Maven Featured Poster
Aranarth 126 Posting Whiz in Training
Fbody 682 Posting Maven Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.