LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01: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, 04: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



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 03:50 PM
Usually happening kariukidw Fedora 1 06-06-2007 11:38 AM
What is happening here? nlinecomputers Linux - Security 10 10-10-2005 01:50 PM
What's Happening? demonfi General 4 08-16-2003 01:40 PM
just a happening aizkorri General 9 11-20-2002 11:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:04 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
Open Source Consulting | Domain Registration