Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-20-2009, 01:12 AM   #1
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:
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.

Old 04-20-2009, 04:55 AM   #2
Registered: Jun 2007
Posts: 118

Rep: Reputation: 17

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.



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 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:25 AM.

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