By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

access error?

P: n/a
Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.


#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */

char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&string[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+current_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;

if (!string[0])
return 0;

printf("\n");

// ************************************************** ********//
// ************************************************** ********//
while (isspace(string[i]))
{
if (string[i] == '\n')
string[i] = ' ';

// ************************************************** ********//
word_len++;
i++;
}

printf("%c",string[i]);
while (string[i] && !isspace(string[i++]))
{
printf("%c",string[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\n\n",
word_wrap(string, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(string,72));
return 0;
}
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Dick D wrote:
Could someone tell me why i get an "access violation" exception when the character at string[i] == newline (0x10) and i try to replace it with a space (0x20)? (see the get_word function)

http://www.eskimo.com/~scs/C-faq/q1.32.html


Brian

Nov 14 '05 #2

P: n/a
Dick D wrote:
Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.
I would prefer payment in advance...
#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */
<OT> Unusual size for tabs </OT>
char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&string[current_len + start_line])))
{
printf("len = %d\n",len);
Please don't use tabs in posts to Usenet, not everyone uses the same tab
stops as you so it messes up the formatting.
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+current_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;
I would not bother with register. Modern optimising compilers are good
at working out where to use registers for variables.
if (!string[0])
return 0;

printf("\n");

// ************************************************** ********//
// ************************************************** ********//
while (isspace(string[i]))
while (isspace((unsigned char)string[i])
Since the is* routines are specified as taking positive values or EOF
and char might be signed giving you negative values.
{
if (string[i] == '\n')
string[i] = ' ';

// ************************************************** ********//
word_len++;
i++;
}

printf("%c",string[i]);
while (string[i] && !isspace(string[i++]))
See above
{
printf("%c",string[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
Main returns an int and takes either 0 or 2 parameters, declare it as
such. Implicit int is no longer valid in C99

int main(void)
{
char *string =
Although the below string is not declared as const it is still not
modifiable and your compiler has sensibly decided to mark the memory it
is in as read only.

You want
char string[] =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\n\n",
word_wrap(string, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(string,72));
return 0;
}


I suggest you google for "comp.lang.c FAQ" and read it. You will
probably find this and many more of your questions answered.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3

P: n/a


Dick D wrote:

Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.

#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */

char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&string[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+current_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;

if (!string[0])
return 0;

printf("\n");

// ************************************************** ********//
// ************************************************** ********//
while (isspace(string[i]))
{
if (string[i] == '\n')
string[i] = ' ';

// ************************************************** ********//
word_len++;
i++;
}

Besides the problems pointed out by others, there is another error in
the above loop.
The program is likely to get an access violation if 'string' does not
contain any whitespace characters, since you increment right past the
end of the string.
printf("%c",string[i]);
while (string[i] && !isspace(string[i++]))
{
printf("%c",string[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\n\n",
word_wrap(string, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(string,72));
return 0;
}


--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.