LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2004, 02:27 PM   #1
Xconsole
LQ Newbie
 
Registered: Apr 2004
Location: 0x#gh
Posts: 2

Rep: Reputation: 0
why char * is called read only memory [C/C++]


why char * is called read only memory. what does it mean ? what is the difference with it from a char array ?
 
Old 04-16-2004, 04:28 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
char * is not read-only. You can write it.
 
Old 04-16-2004, 05:16 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Mara
char * is not read-only. You can write it.
Depends... See difference between:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
	 char *str = "Hi.123456789";

	 strcpy(str, "Changed.");
	 puts(str);
	 return 0;
}
and:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
	 char str[20] = "Hi.123456789";

	 strcpy(str, "Changed.");
	 puts(str);
	 return 0;
}
Xconsole:
Are you trying something like the first example above? If so, the answer to your question is that the string is effectively a constant in this case. But most of the times you can write to a char*, i.e. when there is a memory allocated to it with malloc(), of when it is declared like "char str[]". In that case "str" is also a char*, but you can write to the string through the pointer "str".

Last edited by Hko; 04-16-2004 at 05:18 PM.
 
Old 04-16-2004, 09:56 PM   #4
Xconsole
LQ Newbie
 
Registered: Apr 2004
Location: 0x#gh
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
you can write to a char*, i.e. when there is a memory allocated to it with malloc()
ok, i verified thsi but no result

Code:
       char* str = new char[20]; // memory allocated
         str = "Hi.123456789";

         strcpy(str, "Changed.");
         puts(str);
         return 0;
this gives segmentation fault.

Last edited by Xconsole; 04-16-2004 at 10:37 PM.
 
Old 04-16-2004, 10:52 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
The reason is because pointers point to memory, and char *str points to memory in the data segment which is read only. The difference between char *str="hello" and str[6]="hello" is that while both are declared the same way on the data segment, str[] is treated as readable because it is pushed onto the stack if it is used. *str and str[] are not declared any different by gcc when it creates an executable, the difference is in their use by the programmer. If you want to see what I mean, do a "gcc file.c -S" and view the .s file that is created, it will show you what GCC actually created from your .C
 
Old 04-17-2004, 05:43 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Xconsole
ok, i verified thsi but no result
What do you mean by "no result"? It works, and the char* is writable. Try:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	 char *str;

	 str = malloc(20);
	 if (str == NULL) {
		  fprintf(stderr, "Could not allocatie memory.\n");
		  return 1;
	 }

	 strcpy(str, "Initial string");
	 puts(str);

	 strcpy(str, "Changed.");
	 puts(str);

	 free(str);
	 return 0;
}
Quote:
Originally posted by Xconsole
Code:
       char* str = new char[20]; // memory allocated
         str = "Hi.123456789";

         strcpy(str, "Changed.");
         puts(str);
         return 0;
this gives segmentation fault.
Yes. That was exactly my point!
It is one of the few (the only?) cases where a char* is "read only"....
 
Old 10-22-2010, 08:27 PM   #7
Carunkumar
Member
 
Registered: Sep 2006
Location: chennai
Distribution: fedora core 8
Posts: 106

Rep: Reputation: 15
Quote:
Originally Posted by Xconsole View Post
ok, i verified thsi but no result

Code:
       char* str = new char[20]; // memory allocated
         str = "Hi.123456789";

         strcpy(str, "Changed.");
         puts(str);
         return 0;
this gives segmentation fault.
It will. char * is a variable. If you make it point to something you can't change, you can't change. In this case after making it point to a memory space(new char[]) which can be written to, you made a second statement that made it point to another "constant", which can't be changed. So you get a segmentation fault.
 
Old 10-22-2010, 08:44 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Q: why char * is called read only memory?

A: It *isn't*.
"char *" is a pointer to a character array.
The characters can either be in read-write, or read-only storage. It depends.
Quote:
Q: What is the difference with it from a char array?

A: Effectively, none.
Kernighan and Ritchie (the guys who invented C) explicitly say array and pointers can usually be used interchangably.

There ARE subtle (and important!) differences. And I know somebody's going to yell at me for "over-simplifying" here.

But honestly, for your purposes, you CAN regard pointers and arrays as "pretty much the same thing". At least for the time being.

And, if you want to explore the issue in more depth, then you should Google for "C programming array pointer". Or, better, read Kernighan and Ritchie.
Quote:
Q: ok, i verified this but no result
...
this gives segmentation fault.

A: As Hko replied, that's EXACTLY the point he was trying to make!
See the example below.
Code:
  // This is allocated from the heap with the C++ "new" operator
  // It's read-write
  // You must use "delete" to dispose of it
  char* str1 = new char[20];

  // This is allocated from the heap with the C "malloc()" function
  // It's also read-write
  // You must use "free()" to dispose of it
  char* str2 = malloc (20);

  // This is allocated from the stack
  // It, too, is read-write
  // It's disposed of when then block exits (usually, at the end of the function)
  char* str3[20];

  // This is a constant
  // It is READ-ONLY
  // You MUST NOT try to write to it!
  char* str4 = "Hi.123456789";
'Hope that helps

Last edited by paulsm4; 10-23-2010 at 01:49 AM.
 
  


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
Read a char from a file (PERL) linuxlover1 Programming 4 01-09-2005 09:10 AM
char array of size 10 can read morethan 10 chars!!!!! pippet Programming 13 07-12-2004 01:44 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
C: Best way to read an entire text file and putting it in a *char. Claus Programming 8 03-15-2004 01:22 PM
memory read tincat2 Linux - Hardware 3 08-24-2002 08:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

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