473,769 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats wrong with this pointer?

Dan
Hi, I am getting a SEG V at line 12 and just can't figure out why?
Can you help please? This is a simple program to replace spaces with '_' in string
1 #include <stdio.h>
2
3 int main()
4 {
5 char *ptr;
6 ptr="Beginning Linux Programming.pdf ";
7
8 printf("ptr=%s\ n", ptr);
9 while(*ptr!='\0 ')
10 {
11 if((*ptr)==' ' || (*ptr)==' ')
12 *ptr='_';
13 ptr++;
14 }
15 printf("%s\n",p tr);
16 printf("\n");
17 }
Thanks!

../a.out
ptr=Beginning Linux Programming.pdf
Segmentation fault
Nov 14 '05 #1
5 1399
Dan wrote:
Hi, I am getting a SEG V at line 12 and just can't figure out why?
Can you help please? This is a simple program to replace spaces with '_' in string
1 #include <stdio.h>
2
3 int main()
4 {
5 char *ptr;
6 ptr="Beginning Linux Programming.pdf ";
7
8 printf("ptr=%s\ n", ptr);
9 while(*ptr!='\0 ')
10 {
11 if((*ptr)==' ' || (*ptr)==' ')
12 *ptr='_';
13 ptr++;
14 }
15 printf("%s\n",p tr);
16 printf("\n");
17 }
Thanks!

./a.out
ptr=Beginning Linux Programming.pdf
Segmentation fault


Obviously you've neither read the FAQ nor lurked before posting. Being a
responsible citizen of usenet requires you do both.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
Dan wrote:

Hi, I am getting a SEG V at line 12 and just can't figure out why?
Can you help please? This is a simple program to replace spaces
with '_' in string
1 #include <stdio.h>
2
3 int main()
4 {
5 char *ptr;
6 ptr="Beginning Linux Programming.pdf ";
7
8 printf("ptr=%s\ n", ptr);
9 while(*ptr!='\0 ')
10 {
11 if((*ptr)==' ' || (*ptr)==' ')
12 *ptr='_';
13 ptr++;
14 }
15 printf("%s\n",p tr);
16 printf("\n");
17 }
Thanks!

./a.out
ptr=Beginning Linux Programming.pdf
Segmentation fault


Of course it fails. ptr has been set to point to a non-modifiable
string, which you then attempt to modify. You might try declaring
it as:

ptr[] = "Beginning Linux Programming.pdf ";

which changes it from an initialized pointer to an initialized
array.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3
MJ
Hi
if you do like this it will work fine
#include <stdio.h>

int main()
{
char *ptr;
char ptr1[]="Beginning Linux Programming.pdf ";
ptr = ptr1;

printf("ptr=%s\ n", ptr);
while(*ptr!='\0 ')
{
if((*ptr)==' ' || (*ptr)==' ')
*ptr='_';
ptr++;
}
printf("%s\n",p tr1);
printf("\n");
}
Regards
MJ

Nov 14 '05 #4
MJ wrote:
if((*ptr)==' ' || (*ptr)==' ')


I'm pretty sure that if (*ptr) equals ' ',
then (*ptr) will also be equal to ' '.

--
pete
Nov 14 '05 #5
pete <pf*****@mindsp ring.com> writes:
MJ wrote:
if((*ptr)==' ' || (*ptr)==' ')

I'm pretty sure that if (*ptr) equals ' ',
then (*ptr) will also be equal to ' '.


one of those should be '\t'.

DES
--
Dag-Erling Smørgrav - de*@des.no
Nov 14 '05 #6

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

Similar topics

4
2249
by: Alfonzo Morra | last post by:
I've the ff code in cp assignmenent cstor: PB& PB::operator=( const PB& b) { if ( this != &b ) { PB *pb = new PB( b ) ; this = pb ; // <- Compiler barfs here } return *this ; }
3
2395
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
6
3117
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
5
2834
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of value by casting that functions with appropriate data type?
34
2091
by: Spidey | last post by:
What kind of formating can be done with %p in printf
7
2231
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell me why? Its probably sooooooo obvious, but it is 1.19 am! Thanks. www.thunderin.co.uk/
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
20
2472
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
3
366
by: sam | last post by:
HI, The code is as follows:- #include <stdio.h> 1 2 void * work(void * ptr) 3 { 4 int i; 5 for (i = 0; i < 10; i++) 6 {
0
9590
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
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.