Please look at the following code snippets:
 
char str[]="abcd"; // Gets stored in stack frame.
 
char *str="abcd"; // Where does this gets stored?
 
 
In case of first declaration ie char str[]="abcd"; the string "abcd" gets stored in the stack frame of the invoked funciton. I wonder where does the string gets stored when we declare char *str="abcd"; (Is it in the heap?). Please reply.
 
 
Thanks
Iqbal
WolfPack commented: Improper use of Code Tags +0

Recommended Answers

All 8 Replies

Please post your question outside code tags. I corrected your first two posts, do this one yourself, or I will delete this thread. Since it has been only 2 minutes since your post, you can edit it now.

(Is it in the heap?)

I think both goes on the stack both times. Would only go on the heap/free store if you use the 'new' keyword.

Wolfie ?

p.s.
Yeah and put [ CODE ] [/ CODE ] around JUST the code not the whole post, like Wolpack said please.

>>char *str="abcd";

There are two objects here -- the pointer itself is a variable that is on the stack. It contains the address of the string that is stored in the heap -- often in read-only memory.

[edit]Sorry Holly I didn't see your last post when I posted this. The link you posted is a much better and thorough explaination.[/edit]

[edit]Sorry Holly I didn't see your last post when I posted this. The link you posted is a much better and thorough explaination.[/edit]

He he, well like they always say:
"Big red and firey, always late"

:lol:

Please post your question outside code tags. I corrected your first two posts, do this one yourself, or I will delete this thread. Since it has been only 2 minutes since your post, you can edit it now.

Sorry for my ignorance. As I am new to this blog, I was unaware of the rules.
I tried to edit the post as you suggested, but could not do so.
I will definitely take care of all the rules in my future posts.
I am extremely sorry if I hurt anybody by breaking the rules.
Thanks
Iqbal

Sorry for my ignorance. As I am new to this blog, I was unaware of the rules.
I tried to edit the post as you suggested, but could not do so.
I will definitely take care of all the rules in my future posts.
I am extremely sorry if I hurt anybody by breaking the rules.
Thanks
Iqbal

It's ok it doesn't hurt too much ;) he he
anyway at least we know you got the message now.

Of course we don't expect you to know all the rules straight off. If you look in my signature there's a link to 'the basics', I recommend reading that it will help you to make good posts that will get read and answered. Forums are a bit like Ebay you gotta put some effort into the listing to get bids and a sale, same here to get good answers.

Lastly don't mind WolfPack he's a big softy really, but he has the onerous responsibility of being a moderator and keeping everyone in line and the threads looking neat and tidy, so he has to throw his weight around a litte otherwise everyone would just ignore him, and the forum would be an unsightly mess. The knowledgeable people with all the answers only come and stay and answer posts in nice neat forums so the effort is worth it I think.

Sorry for my ignorance. As I am new to this blog, I was unaware of the rules.
I tried to edit the post as you suggested, but could not do so.
I will definitely take care of all the rules in my future posts.
I am extremely sorry if I hurt anybody by breaking the rules.
Thanks
Iqbal

As long as you learn from your mistakes, everyone is happy about it. And btw welcome to the forums. Just be kind enough to respect other ppl out here and i am sure you would get all the help you require.

Keep rocking....

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.