473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reassigning value of a pointer

Hi all,

In the following code I am trying to change the contents of a string:

int main()
{
char *string="testin g";
rename(string);

return 0;
}

void rename(char *s)
{
printf("Charact er 1: %c\n",*s);
*s='a';
printf("Charact er 1 now: %c\n",*c);
}

Anyway, I am sure this is possible but for some reason I'm getting
segmentation faults at line *s='a'; when I attempt to change the value
in which *s is currently pointing to, being the first character in the
string.

Any ideas?

Thanks in advance,

Ben.

Nov 15 '05 #1
20 5449
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<bh******@dodo. com.au> wrote:
In the following code I am trying to change the contents of a string: int main()
{
char *string="testin g";


The standard allows string literals to be stored in read-only memory.
An assignment of a string literal to a pointer sets the pointer value
to the address of that {possibly read-only} memory.

In particular, char *string="testin g"; usually does not allocate some
memory somewhere and copy the string into it at runtime. That's
one of the possible behaviours, but it isn't the only possible
behaviour.

Another thing to note is that it is allowed for the compiler
to merge all string literals -- so for example if you also had

char *anotherstring= "testing";

then anotherstring could end up as the same pointer value as
your string variable. Furthermore, if you had

char *thirdstring="j ust testing";

then string and anotherstring could end up pointing at the terminal
"testing" substring of the "just testing".

If you need a modifiable string, allocate the memory (somehow) and
copy the appropriate contents into it.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Nov 15 '05 #2
Thanks for that information Walter. I managed to get it working, by
allocating the memory then using strcpy to load the string.

Regards,

Ben.

Nov 15 '05 #3
bh******@dodo.c om.au wrote:
Hi all,

In the following code I am trying to change the contents of a string:
Then don't use string literals.

int main()
{
char *string="testin g";


If you want to change the string, don't make 'string' point to a literal
constant; use an array:
char string[] = "testing";

Always check the FAQ before posting. In this case, you might have found
<http://www.eskimo.com/~scs/C-faq/q1.32.html> useful.
Nov 15 '05 #4
in the above program what is "c", not declared in the program, first u
declare that and assign a value and then print it ok. i think that
there is no problem in the line *s='a';
so try it and send me the result.

Nov 15 '05 #5
Martin Ambuhl wrote:
Then don't use string literals. If you want to change the string,
don't make 'string' point to a literal constant;
There is no such thing as a "literal constant" in C.
char string[] = "testing";


You just used a string literal to initialize an array.

--
pete
Nov 15 '05 #6
bh******@dodo.c om.au wrote:
int main()
{
char *string="testin g";
rename(string);


Your immediate problem is as others have explained it. However, you have
another bug: rename() is already a Standard function, and you cannot
redefine it, let alone redefine it with an incompatible declaration.

Richard
Nov 15 '05 #7
It becomes a standard function iff (if and only if) you include
<stdio.h>. Although dubbed as *Standard* Input Ouput - not really a
standard. Any programmer could redefine functions already defined in
*Standard* headers. Other than that, what the original author intended
to do clearly shouldn't be named as rename(..); rather,
change_sptr(... ); Or something simliar.

Nov 15 '05 #8
David G. Hong wrote:

It becomes a standard function iff (if and only if) you include
<stdio.h>.


How do you figure that?

N869
7.1.3 Reserved identifiers
[#1]
-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.

--
pete
Nov 15 '05 #9
On Wed, 28 Sep 2005 06:34:39 +0000, pete wrote:
Martin Ambuhl wrote:
Then don't use string literals.

If you want to change the string,
don't make 'string' point to a literal constant;


There is no such thing as a "literal constant" in C.


It is a resonable description of a string literal object.
char string[] = "testing";


You just used a string literal to initialize an array.


which solves the primary problem in the original code. I'm not clear what
point you are trying to make here.

Lawrence

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2049
by: Ken Godee | last post by:
I'm using the python Queue module, but since I can not find anyway to clear the Queue, I thought I would try to reassign ie...... q1 = Queue.Queue() q1.put('test1') q1.put('test2') q1.qsize() 2 q1 = Queue.Queue()
2
1419
by: webdev | last post by:
I'm using the code shown below which displays 3 input fields - when you click on an input field, a DIV is displayed which allows you to select a task. On clicking the task, it is inserted into the appropriate input field, and the DIV is hidden again. I'm using a tooltip script from Dynamic Drive (ommitted for clarity) to show the task description over the input field, but I am unable to reassign a new function to the onmouseover event...
4
5401
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
3
1327
by: biswaranjan.rath | last post by:
Can i do something like this: <xsl:variable name="varMaxDiffId"> <xsl:choose> <xsl:when test="$varMaxDiffId == 'NULL'"> <xsl:value-of select="-1"/> </xsl:when> <xsl:when test="$varMaxDiffId < @unique_diff_id"> <xsl:value-of select="@unique_diff_id"/> </xsl:when>
23
2197
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
40
9790
by: Zach | last post by:
Can someone please explain what this means and illustrate the difference with some code. Thanks, Zach
4
2296
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. My interpretation: 1. Essentially pushes the value type on the stack
6
2100
by: woger151 | last post by:
If I write $x = new some_class($init_data1); and then $x = new some_class($init_data2); does the first object (constructed with $init_data1) get destroyed, or do I have to call unset() for that? And am I guaranteed that after the second call the value of $x will be as if the first call was never made?
6
1398
by: Tobe | last post by:
Hi, Here's an example of something that feels like it should be OK but does in fact produce a segfault on every compiler I've tried (VC2005, g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked // KABOOM is the one that segfaults. If I change the references to pointers (and make the appropriate dereferences) everything works just fine so is this some finer point of references I'm not grasping or an issue with the STL ?
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.