Why error in this code??? char*

M

Mars

#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');
}
 
E

E. Robert Tisdale

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
 
J

John Valko

Mars said:
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
 
J

John Valko

E. Robert Tisdale said:
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
 
E

Emmanuel Delahaye

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"
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

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 >= 'a')
{
s = s - 'a' + 'A';
putchar (s);
}
else
{
putchar (s);
}
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top