pointer probs?
c++
hi im having problem with pointers, i think
i have a number of questions, any help would be great!
when u declare :
char *ptrstr = "hello world!!";
where is the string stored? is this even valid code? why wouldnt i have to do this:
char str[20] = {"hello world\0"}
char *ptrStr = str;
??????
during declaration and execution, why can i ever do this?!
ptrstr = "hello world\0"; // !?!?!?!?!!?
how is this string alloted,where is it at, why can i do
ptrstr = "adasd", and not str = "asdasds", where str is an array ??
when u set ptrstr = "something else", what happens to the old stuff?
also, what is the difference between the * and [] operators in functions? ex: void func(char *str, char str1[]);
i realize they do different things, but why?
evilcrapx@hotmail.com
thanks!
Well.. take a look at this code:
No problem there right? Well.. if you think about it, that code is the same thing as this:
The reason this works is the same reason the first code example works. When you do something like that, it automatically allocates some temporary memory that is freed when the variable goes out of scope (please correct me if I'm wrong on this).
Also, the reason you can't do something like this:
is because text is a static pointer and you cannot change what a static pointer points to.
Anyway.. hope that helps.
- Ian Perez (fett@willcomp.com) - "It is by will alone I set my mind in motion"
Edited by - [bobafet] on May 2, 2001 12:32:51 AM
|
No problem there right? Well.. if you think about it, that code is the same thing as this:
|
The reason this works is the same reason the first code example works. When you do something like that, it automatically allocates some temporary memory that is freed when the variable goes out of scope (please correct me if I'm wrong on this).
Also, the reason you can't do something like this:
|
is because text is a static pointer and you cannot change what a static pointer points to.
Anyway.. hope that helps.
- Ian Perez (fett@willcomp.com) - "It is by will alone I set my mind in motion"
Edited by - [bobafet] on May 2, 2001 12:32:51 AM
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
char *pstr = "something";
pstr is a pointer to the address that "something"
starts at say 0x00250234 because you know the size of the string the correct memory can be allocated sequetialy. the 'o' would be at address 0x00250235 and so on the final character would be the null character constant which of course signals the end of the string.
char array[20] = "something";
this will go ahead and allocate enough space for 20 characters to which array or array[0] contains the address of the first character and array[1] in this case 'o' would be the next address.
so they can be looked at as basically the same thing but array[0] is the actual adress of the first character where pstr holds the address of where the first char is stored.
so to then say: char array[20] = "something";
char *pArray = array;
says pArray points to &array[0] (the address of first element in the array)
Hope my wording is understandable.
Edited by - Scooter on May 2, 2001 12:42:58 AM
pstr is a pointer to the address that "something"
starts at say 0x00250234 because you know the size of the string the correct memory can be allocated sequetialy. the 'o' would be at address 0x00250235 and so on the final character would be the null character constant which of course signals the end of the string.
char array[20] = "something";
this will go ahead and allocate enough space for 20 characters to which array or array[0] contains the address of the first character and array[1] in this case 'o' would be the next address.
so they can be looked at as basically the same thing but array[0] is the actual adress of the first character where pstr holds the address of where the first char is stored.
so to then say: char array[20] = "something";
char *pArray = array;
says pArray points to &array[0] (the address of first element in the array)
Hope my wording is understandable.
Edited by - Scooter on May 2, 2001 12:42:58 AM
We are here just like this,It really doesn't matter why.The world is here like it is,'We could laugh we could cry.Is it good or is it bad,Wright or maybe wrong?There's no use of being sad,Without a purpose within a song.
Correct =)!
char *mystring="something";
char *anotherptr=&mystring //points to the address of mystring
good luck
char *mystring="something";
char *anotherptr=&mystring //points to the address of mystring
good luck
The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.
?Have a nice day!?
quote:
Original post by GoofProg
Correct =)!
char *mystring="something";
char *anotherptr=&mystring //points to the address of mystring
good luck
I think you mean this:
|
- Ian Perez (fett@willcomp.com) - "It is by will alone I set my mind in motion"
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
thank you, ian perez,
considering this:
char *ptrstr = "hello world";
ptrstr = "a diff string thats longer";
is doing that basically the same as:
char *ptrstr = "hello world";
delete ptrstr;
ptrstr = new char[35];
...
/// ???
????
considering this:
char *ptrstr = "hello world";
ptrstr = "a diff string thats longer";
is doing that basically the same as:
char *ptrstr = "hello world";
delete ptrstr;
ptrstr = new char[35];
...
/// ???
????
quote:
Original post by EvilCrap
char *ptrstr = "hello world";
ptrstr = "a diff string thats longer";
is doing that basically the same as:
char *ptrstr = "hello world";
delete ptrstr;
ptrstr = new char[35];
Hmm.. not quite.. you can''t delete ptrstr because it wasn''t allocated with the new operator.. it''s more of an automatic allocation/deallocation. Your first code example is fine though.
- Ian Perez (fett@willcomp.com) - "It is by will alone I set my mind in motion"
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
i thought all hard-coded strings were compiled in some sort of string table in your exe, so
char *ptrstr = "hello world!!";
declares a pointer to the location of "hello world!!" in the string table, that also explains why you can't delete or modify the string
i'm not sure though...
Edited by - kvh on May 3, 2001 4:45:04 PM
char *ptrstr = "hello world!!";
declares a pointer to the location of "hello world!!" in the string table, that also explains why you can't delete or modify the string
i'm not sure though...
Edited by - kvh on May 3, 2001 4:45:04 PM
the easiest way to learn is to try some examples and see which addresses are allocated and how they are referenced.
Yes! There are kangaroos in Australia but I haven't seen them...yet
What you are talking about here is the difference between ''string literals'' and allocated string space.
A string literal can be of a number of different forms - at any rate it distinctly different from declaring a string in code using a char array.
As the compiler goes through the code you provide, it identifies all the string literals and places them in a special area just for that.
So when you say
char* pString = "Test String";
you are saying that pString is a pointer variable that points to the memory addess that the compiler has assigned to the string literal "Test String."
C and some older C++ compilers actually allow you to modify the contents of this memory - VERY nasty practice. Don''t do it! If you need to modify it, then assign it to a char array or std::string and use that instead. You could unintentionally overwrite some of the code which is int he same area of memory - which would produce very bizzare and hair-pulling type bugs.
One last thing about string literals which I thought was interesting - Quoting from Stroustrup
"Whether two identical string literals are allocated as one is implementation-defined."
What this means is if i do the following:
char* pString = "Test String";
char* pString1 = "Test String";
On some platforms the two char pointers will contain the same address, while on others they won''t.
A string literal can be of a number of different forms - at any rate it distinctly different from declaring a string in code using a char array.
As the compiler goes through the code you provide, it identifies all the string literals and places them in a special area just for that.
So when you say
char* pString = "Test String";
you are saying that pString is a pointer variable that points to the memory addess that the compiler has assigned to the string literal "Test String."
C and some older C++ compilers actually allow you to modify the contents of this memory - VERY nasty practice. Don''t do it! If you need to modify it, then assign it to a char array or std::string and use that instead. You could unintentionally overwrite some of the code which is int he same area of memory - which would produce very bizzare and hair-pulling type bugs.
One last thing about string literals which I thought was interesting - Quoting from Stroustrup
"Whether two identical string literals are allocated as one is implementation-defined."
What this means is if i do the following:
char* pString = "Test String";
char* pString1 = "Test String";
On some platforms the two char pointers will contain the same address, while on others they won''t.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement