Is this OK? If not, why?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 32767
#define DIGITS 5 /* number of decimal digits in MAXLEN */
int main(void)
{
size_t i;
int j;
char word[MAXLEN];
char format[DIGITS+3];
int counts[MAXLEN];
size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
word[MAXLEN - 1] = '\0';
sprintf(format, "%%%ds", MAXLEN);
while (scanf(format, word) 0) {
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
scanf("%*s");
}
len_curr = strlen(word);
counts[len_curr]++;
if (len_curr max_curr)
max_curr = len_curr;
}
for (i=1; i<=max_curr; i++) {
printf("%*d|", DIGITS, (int)i);
for (j=0; j<counts[i]; j++)
putchar('=');
putchar('\n');
}
return 0;
}
--
char s[]="\16Jsa ukenethr ,cto haCr\n";int main(void){*s*=5;*
s%=23;putchar(s[0][s]);return*s-14?main():!putchar(9[s+*s]);} 7 1397
Army1987 wrote:
Is this OK? If not, why?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 32767
#define DIGITS 5 /* number of decimal digits in MAXLEN */
int main(void)
{
size_t i;
int j;
char word[MAXLEN];
char format[DIGITS+3];
int counts[MAXLEN];
size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
memset(counts, 0, sizeof counts);
word[MAXLEN - 1] = '\0';
sprintf(format, "%%%ds", MAXLEN);
This format string will cause scanf() to write in word[MAXLEN],
which is off by one.
while (scanf(format, word) 0) {
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
scanf("%*s");
}
len_curr = strlen(word);
counts[len_curr]++;
if (len_curr max_curr)
max_curr = len_curr;
}
for (i=1; i<=max_curr; i++) {
Broken loop? Can max_curr ever equal MAXLEN? If so, you have an off by
one error a couple of lines down.
printf("%*d|", DIGITS, (int)i);
for (j=0; j<counts[i]; j++)
putchar('=');
putchar('\n');
}
return 0;
}
HTH
Bjørn
"Bjørn Augestad" <bo*@metasystems.noha scritto nel messaggio
news:Zq*********************@telenor.com...
Army1987 wrote:
>Is this OK? If not, why?
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 32767 #define DIGITS 5 /* number of decimal digits in MAXLEN */ int main(void) { size_t i; int j; char word[MAXLEN]; char format[DIGITS+3]; int counts[MAXLEN]; size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
memset(counts, 0, sizeof counts);
> word[MAXLEN - 1] = '\0'; sprintf(format, "%%%ds", MAXLEN);
This format string will cause scanf() to write in word[MAXLEN], which
is off by one.
I do check this in the following if, but I'd forgotten that scanf
doesn't know about sizeof word, so it'll try to zero word[MAXLEN]
anyway... Unluckily (or luckily, depending on the point of view)
when I tested the program (using 10 as MAXLEN) it didn't contain
sensible data, neither did any demon fly out of my nose.
Too tired to fix that right now...
> while (scanf(format, word) 0) { if (word[MAXLEN-1] != '\0') { word[MAXLEN-1] = '\0'; fprintf(stderr, "Truncating word longer than %d " "characters to %s.\n", MAXLEN-1, word); scanf("%*s"); } len_curr = strlen(word); counts[len_curr]++; if (len_curr max_curr) max_curr = len_curr; } for (i=1; i<=max_curr; i++) {
Broken loop? Can max_curr ever equal MAXLEN? If so, you have an off by one
error a couple of lines down.
No, it can't, since word[MAXLEN-1] is zeroed above, so strlen(word)
can at most be MAXLEN-1.
> printf("%*d|", DIGITS, (int)i); for (j=0; j<counts[i]; j++) putchar('='); putchar('\n'); } return 0; }
"Army1987" <pl********@for.itha scritto nel messaggio
news:f0**********@tdi.cu.mi.it...
"Bjørn Augestad" <bo*@metasystems.noha scritto nel messaggio
news:Zq*********************@telenor.com...
>Army1987 wrote:
>>Is this OK? If not, why?
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 32767 #define DIGITS 5 /* number of decimal digits in MAXLEN */ int main(void) { size_t i; int j; char word[MAXLEN];
char word[MAXLEN + 1];
>> char format[DIGITS+3]; int counts[MAXLEN]; size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
memset(counts, 0, sizeof counts);
>> word[MAXLEN - 1] = '\0'; sprintf(format, "%%%ds", MAXLEN);
This format string will cause scanf() to write in word[MAXLEN], which is off by one.
I do check this in the following if, but I'd forgotten that scanf
doesn't know about sizeof word, so it'll try to zero word[MAXLEN]
anyway... Unluckily (or luckily, depending on the point of view)
when I tested the program (using 10 as MAXLEN) it didn't contain
sensible data, neither did any demon fly out of my nose.
Too tired to fix that right now...
I hadn't realized this was that trivial (see above)...
>> while (scanf(format, word) 0) { if (word[MAXLEN-1] != '\0') { word[MAXLEN-1] = '\0'; fprintf(stderr, "Truncating word longer than %d " "characters to %s.\n", MAXLEN-1, word); scanf("%*s");
....but here's another problem. If a word is exactly MAXLEN
characters long, this scanf discards the following word. (I could
check this by setting word[MAXLEN] to some nonzero value and check
it before the second scanf.)
Army1987 wrote:
>
Is this OK? If not, why?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 32767
#define DIGITS 5 /* number of decimal digits in MAXLEN */
int main(void)
{
size_t i;
int j;
char word[MAXLEN];
char format[DIGITS+3];
int counts[MAXLEN];
Right here you have allocated more than 65535 (unless sizeof(int)
== 1) bytes, which is not guaranteed (but may be possible). For
C90 that limit is 32767. You should malloc such beasts.
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net
--
Posted via a free Usenet account from http://www.teranews.com
"CBFalconer" <cb********@yahoo.comha scritto nel messaggio
news:46***************@yahoo.com...
Army1987 wrote:
>> Is this OK? If not, why?
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 32767 #define DIGITS 5 /* number of decimal digits in MAXLEN */ int main(void) { size_t i; int j; char word[MAXLEN]; char format[DIGITS+3]; int counts[MAXLEN];
Right here you have allocated more than 65535 (unless sizeof(int)
== 1) bytes, which is not guaranteed (but may be possible). For
C90 that limit is 32767. You should malloc such beasts.
Or reduce MAXLEN (I wanted a more realistic limit than 10, but now
I realize it is excessive...)
"Army1987" <pl********@for.itha scritto nel messaggio
news:f0**********@tdi.cu.mi.it...
>>Army1987 wrote: while (scanf(format, word) 0) { if (word[MAXLEN-1] != '\0') { word[MAXLEN-1] = '\0'; fprintf(stderr, "Truncating word longer than %d " "characters to %s.\n", MAXLEN-1, word); scanf("%*s");
...but here's another problem. If a word is exactly MAXLEN
characters long, this scanf discards the following word. (I could
check this by setting word[MAXLEN] to some nonzero value and check
it before the second scanf.)
Useless anyway. It'll be zeroed regardless of wheter the word is
exactly MAXLEN characters or longer.
#include <ctype.h>
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
if (!isspace(getchar())
scanf("%*s");
}
should work, shouldn't it?
"Army1987" <pl********@for.itha scritto nel messaggio
news:f0**********@tdi.cu.mi.it...
#include <ctype.h>
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
if (!isspace(getchar())
)
scanf("%*s");
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Readle |
last post by:
Ok, I've just recently finished a beginning C class and now I'm working
through K&R2 (alongside the C99 standard) to *really* learn C.
So anyway, I'm working on an exercise in chapter one which...
|
by: Josh Zenker |
last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?
#include <stdio.h>
/* copies input to output, printing */
/* series of...
|
by: arnuld |
last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2:
http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17
i see this uses pointers whereas K&R2 have not discussed pointers...
|
by: arnuld |
last post by:
i have created a solutions myself. it compiles without any trouble
and runs but it prints some strange characters. i am not able to find
where is the trouble.
...
|
by: arnuld |
last post by:
as i said, i have restarted the book because i overlooked some
material. i want to have some comments/views on this solution. it runs
fine, BTW.
------------------ PROGRAMME --------------
/*...
|
by: arnuld |
last post by:
i have created solution which compiles and runs without any error/
warning but it does not work. i am not able to understand why. i
thought it is good to post my code here for correction before...
|
by: arnuld |
last post by:
this programme runs without any error but it does not do what i want
it to do:
------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.
STATEMENT:
Write a program...
|
by: arnuld |
last post by:
this is a programme that counts the "lengths" of each word and then
prints that many of stars(*) on the output . it is a modified form of
K&R2 exercise 1-13. the programme runs without any...
|
by: arnuld |
last post by:
i am not able to make it work. it compiles without any error but does
not work:
what i WANTED:
1.) 1st we will take the input in to an array. (calling "getline" in
"main")
2.) we will print...
|
by: santosh |
last post by:
Hello all,
In K&R2 one exercise asks the reader to compute and print the limits for
the basic integer types. This is trivial for unsigned types. But is it
possible for signed types without...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |