LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-20-2009, 02:12 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Smile Why is this happening...??


There is an important difference between these definitions:
Code:
char amessage[] = "now is the time"; /* an array */
char *pmessage = "now is the time"; /* a pointer */
amessage is an array, just big enough to hold the sequence of characters and '\0' that
initializes it. Individual characters within the array may be changed but amessage will always
refer to the same storage. On the other hand, pmessage is a pointer, initialized to point to a
string constant; the pointer may subsequently be modified to point elsewhere, but the result is
undefined if you try to modify the string contents.


Please explain to me why this difference occurs. It would be better if the explanation is based on the memory allocation.

thanks,
thelink
 
Old 04-20-2009, 05:55 AM   #2
greplinux
Member
 
Registered: Jun 2007
Posts: 118

Rep: Reputation: 17
Hi,

In the declaration

/* char *pmessage = "now is the time"; */

you are pointing to a string constant, which cannot be changed. That is why you are getting memory access error while trying to alter its contents.

Thanks,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is of the happening? crazyness003 LinuxQuestions.org Member Intro 2 12-29-2007 04:50 PM
Usually happening kariukidw Fedora 1 06-06-2007 12:38 PM
What is happening here? nlinecomputers Linux - Security 10 10-10-2005 02:50 PM
What's Happening? demonfi General 4 08-16-2003 02:40 PM
just a happening aizkorri General 9 11-20-2002 12:08 PM


All times are GMT -5. The time now is 10:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration