entering more characters

K

kk

Hi all,

in the following code, i enter more characters into a character array.
array size declared as 80. i enter more characters then i get
segmentation fault. if anybody knows give me reply.
thanks in advance.

Regards
kk


main()
{
char s[80];
scanf("%[^\n]s",s);
printf("%s",s);
}
 
S

Suman

kk said:
Hi all,

in the following code, i enter more characters into a character array.
array size declared as 80. i enter more characters then i get
segmentation fault. if anybody knows give me reply.
Are you entering more than 80 characters?
Writing past array bounds is your problem.
thanks in advance.

Regards
kk


main()
{
char s[80];
scanf("%[^\n]s",s);
printf("%s",s);
}
 
S

Suman

Suman said:
Are you entering more than 80 characters?
Ah! Off by one! You can't have more than 79 characters in your
`s' thingy.
Writing past array bounds is your problem.
thanks in advance.

Regards
kk


main()
{
char s[80];
scanf("%[^\n]s",s);
printf("%s",s);
}
The following might be of interest to you:
http://groups.google.co.in/group/comp.lang.c/browse_frm/
thread/4612b24419b17c78/27bbc52f5be7f3be?
q=pete+%2B+scanf&rnum=6&hl=en#27bbc52f5be7f3be
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi all,

in the following code, i enter more characters into a character array.
array size declared as 80. i enter more characters then i get
segmentation fault. if anybody knows give me reply.
thanks in advance.

A truism that my mother taught me applies here...

"You cant stuff a size 10 foot into size 7 shoes. Either the shoes split
or your foot does."

In this case, you can't stuff more than 80 chars into an 80 char array.
If you do, you write over something (what, we cannot tell you) outside
of the array, and cause your program to fall over.

In other words (to quote Mel Brooks) "Don't do that."


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC54OMagVFX4UWr64RAjdCAKDU7DZ4Ot0WxHF5vRZRCP2iG8VqBACfd7HD
nRoS+pj0njgGDI/r4rgynpU=
=xJza
-----END PGP SIGNATURE-----
 
W

Walter Roberson

in the following code, i enter more characters into a character array.
array size declared as 80. i enter more characters then i get
segmentation fault. if anybody knows give me reply.
thanks in advance.
main()
{
char s[80];
scanf("%[^\n]s",s);
printf("%s",s);
}


I -suspect- that the answer you are looking for is:

#include <stdio.h>
int main(void) {
char s[80];
scanf("%79[^\n]", s);
printf("%s",s);
}


Note that if you use this and the input stream has more than 79
characters before the newline, then the remaining characters will
remain in the input buffer. Also, watch out because even if a newline
was supplied, it is going to remain in the input buffer.

Also note that the format specifier ends at the ] and that the
s that you had after that would be treated as a literal character
to be matched -- the format specifier is not %[something]s
just %[something]
 
J

Jack Klein

comp.lang.c:

1. Stop top posting.
Why don't u do a bounce check? never get past an array limit

2. Don't post nonsense.

-- 'u' doesn't post here anymore, we kicked him out.

-- C doesn't have anything called a "bounce check".

-- There is no way to stop scanf "%s" without a length value from
overflowing a small buffer. On some implementations, there is no way
to prevent it from overflowing any size buffer.
kk said:
Hi all,

in the following code, i enter more characters into a character array.
array size declared as 80. i enter more characters then i get
segmentation fault. if anybody knows give me reply.
thanks in advance.

Regards
kk


main()
{
char s[80];
scanf("%[^\n]s",s);
printf("%s",s);
}
 

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

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,911
Members
47,453
Latest member
MadelinePh

Latest Threads

Top