473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why error in this code??? char*

#include <stdio.h>
#include <string.h>

int main(void)
{
char* name="Smith SYN is a man.";
char* sure="SyN";
char* s;
int x,y;

y=x=strlen(sure);

s=strstr(name,sure);

for (;x>0;x--)
if (s[y-x]>='a')
{
s[y-x]=s[y-x]-'a'+'A';
putchar(s[y-x]);
}
else
putchar(s[y-x]);
putchar('\n');
}
Nov 14 '05 #1
5 2212
Mars wrote:
char* name="Smith SYN is a man.";
char* sure="SyN";

s=strstr(name,sure);
s = NULL; (strstr() is case sensitive). And since most of the time you
won't be doing it on strings you define you should make a practice of
checking what it returns.

for (;x>0;x--)
if (s[y-x]>='a')
{
s[y-x]=s[y-x]-'a'+'A';

Boom. First, s is NULL. Second, if it had pointed somewhere inside of
name, changing its contents would be illegal since you can't (even if it
happens to work for you) modify string literals. You can fix this by
declaring it as a char array, instead of pointer to char.

Hope that helps,
John
Nov 14 '05 #2
> cat main.c
#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
const
char* name = "Smith SYN is a man.";
const
char* sure = "SYN";
int x = strlen(sure);
int y = x;

char* s = strstr(name, sure);

for (;0 < x; --x)
if ('a' <= s[y-x]) {
s[y-x] = s[y-x] - 'a' + 'A';
putchar(s[y-x]);
}
else
putchar(s[y-x]);

putchar('\n');
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

SYN

Nov 14 '05 #3
E. Robert Tisdale wrote:
> cat main.c #include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
const
char* name = "Smith SYN is a man.";
const
char* sure = "SYN";
int x = strlen(sure);
int y = x;

char* s = strstr(name, sure);

for (;0 < x; --x)
if ('a' <= s[y-x]) {

The following code is never executed: s[y-x] = s[y-x] - 'a' + 'A';
putchar(s[y-x]); ------------------------------------- }
else
putchar(s[y-x]);

putchar('\n');
return 0;
}


For the sake of argument I changed your if condition so that the code
would execute:
if('A' <= s[y-x])

jvalko@the-slack-beast:~/junk$ gcc -o -ansi -pedantic -Wall -O2 -o main
main.c
jvalko@the-slack-beast:~/junk$ ./main
Segmentation fault

You can't modify string literals.
see FAQ 16.6 http://www.eskimo.com/~scs/C-faq/q16.6.html
and FAQ 6.2 http://www.eskimo.com/~scs/C-faq/q6.2.html

--John
Nov 14 '05 #4
Mars wrote on 25/02/05 :

Your code commented (-ed-)

#include <stdio.h>
#include <string.h>

int main (void)
{
/* -ed-
* String literals are non-writable.
* Better to define them 'const'.
*/
char const *name = "Smith SYN is a man.";
char const *sure = "SyN";

/* -ed-
* To prevent temptations
* (because 's' is going to point to the string pointed by 'name'
*/
char const *s;
int x, y;

y = x = strlen (sure);

s = strstr (name, sure);
/* -ed-
* strstr() can fail.
* (Actually it does, because "SyN" is not "SYN",
* hence s is NULL and undefined behaviour
* when attempting to use it...
*/

for (; x > 0; x--)
{
/* -ed- missing {} are considered dangerous... */
if (s[y - x] >= 'a')
{

/* -ed-
* Now, we have a nice warning there :

main.c: In function `main':
main.c:29: warning: assignment of read-only location

* modifying is string literal invokes an undefined
behaviour...
* IOW, it's a bug. Don't do that, or use array of char
instead
* (and remove the 'const' qualifiers)
*/
s[y - x] = s[y - x] - 'a' + 'A';

/* -ed-
* I'm not sure what you are doing here.
* Note that 'a' + 'A' is meaningless and not portable
*/
putchar (s[y - x]);
}
else
{
putchar (s[y - x]);
}

/* -ed-
* It is silly to evaluate 4 times y - x when it is invariant.
* Do it once and stick the result in some local...
*/
}
putchar ('\n');

/* -ed- [C90] missing return some portable value */
return 0;
}

Try this :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main (void)
{
char name[] = "Smith SYN is a man.";
char sure[] = "SYN"; /* SYN is not SyN... */
char *s = strstr (name, sure);

if (s != NULL)
{
int x, y;

y = x = strlen (sure);
for (; x > 0; x--)
{
if (s[y - x] >= 'a')
{
s[y - x] = s[y - x] - 'a' + 'A';
putchar (s[y - x]);
}
else
{
putchar (s[y - x]);
}
}
putchar ('\n');
}

/* Dev-c++ trick */
system ("pause");
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #5
(supersedes <mn***********************@YOURBRAnoos.fr>)

Mars wrote on 25/02/05 :

Your code commented (-ed-)

#include <stdio.h>
#include <string.h>

int main (void)
{
/* -ed-
* String literals are non-writable.
* Better to define them 'const'.
*/
char const *name = "Smith SYN is a man.";
char const *sure = "SyN";

/* -ed-
* To prevent temptations
* (because 's' is going to point to the string pointed by 'name'
*/
char const *s;
int x, y;

y = x = strlen (sure);

s = strstr (name, sure);
/* -ed-
* strstr() can fail.
* (Actually it does, because "SyN" is not "SYN",
* hence s is NULL and undefined behaviour
* when attempting to use it...
*/

for (; x > 0; x--)
{
/* -ed- missing {} are considered dangerous... */
if (s[y - x] >= 'a')
{

/* -ed-
* Now, we have a nice warning there :

main.c: In function `main':
main.c:29: warning: assignment of read-only location

* modifying is string literal invokes an undefined
behaviour...
* IOW, it's a bug. Don't do that, or use array of char
instead
* (and remove the 'const' qualifiers)
*/
s[y - x] = s[y - x] - 'a' + 'A';

/* -ed-
* I'm not sure what you are doing here.
* Note that 'a' + 'A' is meaningless and not portable
*/
putchar (s[y - x]);
}
else
{
putchar (s[y - x]);
}

/* -ed-
* It is silly to evaluate 4 times y - x when it is invariant.
* Do it once and stick the result in some local...
*/
}
putchar ('\n');

/* -ed- [C90] missing return some portable value */
return 0;
}

Try this :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main (void)
{
char name[] = "Smith SYN is a man.";
char sure[] = "SYN"; /* SYN is not SyN... */
char *s = strstr (name, sure);

if (s != NULL)
{
int x, y;

y = x = strlen (sure);
for (; x > 0; x--)
{
int const i = y - x;

if (s[i] >= 'a')
{
s[i] = s[i] - 'a' + 'A';
putchar (s[i]);
}
else
{
putchar (s[i]);
}
}
putchar ('\n');
}

/* Dev-c++ trick */
system ("pause");
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #6

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

Similar topics

4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
3
by: Gizmo | last post by:
hello all have been trying to write a Mid() function a bit like the one in vb. i come to compile it and there are no errors however when i run it an error accours and it says the program has to...
11
by: muser | last post by:
In the code I supplied before this one, the cause of the problem is an access violation error. When I run the debugger it skips into what I can only assume is the compilers version of my code. And...
0
by: Morten Gulbrandsen | last post by:
C:\mysql\bin>mysql -u elmasri -pnavathe company Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 to server version: 4.1.0-alpha-max-debug Type...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
6
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get...
2
by: cr55 | last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have...
5
by: GaryE | last post by:
Hello: I am having trouble linking a couple of files using the boost::filesystem. I am using MSVC 6.0. Here is an abbreviated version of my problem: foo.h: #ifndef __FOO_ #define...
4
by: Protoman | last post by:
Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.