Friday, October 24, 2008

pointer pitfall

Some C pointer fundamentals manage to fox even seasoned programmers.
For example, what is wrong with the following piece of code:

func() {

char *c;
c = "this is a string.";
*c = 'T';
printf("%s", c);
}


The code tries to capitalize the first letter of the string. It looks correct at first glance, but is not. It might even compile without any error, but will throw out an error when run.

13 comments:

Rand said...

Of course it will generate an error. Variable "c" is a pointer to the string and that pointer gets assigned to the address of a string constant. String constants aren't writable so you'll get an error when you try to modify it.

rtra said...

Actually, modifying a string constant is undefined behavior. There's a lot of platforms where that code works as expected.

Anonymous said...

Also, note why the string is a constant in this case? the string doesn't just belong to the pointer, but it might as well have been in use by some other variable - eg some compiler optimization might have resulted in the same string getting used by more than one pointer. as a result it is made a string constant and modifying it is an undefined behavior and wrong programming.

Sathya G said...

It is very useful to read about the computers, programming and operating system. thank you for sharing the blog with us

Suseela Susiee said...


Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


SEO Company in Chennai

Sathya G said...

the blog is very interesting and will be much useful for us. thank you for sharing the blog with us. please keep on updating.
ROI Services in Chennai

Suseela Susiee said...


All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.


Online Reputation Management

Saki R said...


nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.

Java Training in Chennai

Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
Digital Marketing Company in India

Shalini said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
seo company in india

Aasha said...


This blog explains the details about changing the ways of doing that business. That is understand well and doing some different process. Provides he best output of others. Thanks for this blog.
web Design Company in Chennai

Karthika Shree said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
Java Training in Chennai

Balaji said...

Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
Analytics Training In Chennai