access error?

D

Dick D

Could someone tell me why i get an "access violation" exception when the
character at string == 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))
{
if (string == '\n')
string = ' ';

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

printf("%c",string);
while (string && !isspace(string[i++]))
{
printf("%c",string);
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;
}
 
F

Flash Gordon

Dick said:
Could someone tell me why i get an "access violation" exception when the
character at string == 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 */

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))


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

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

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


See above
{
printf("%c",string);
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.
 
F

Fred L. Kleinschmidt

Dick said:
Could someone tell me why i get an "access violation" exception when the
character at string == 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))
{
if (string == '\n')
string = ' ';

// **********************************************************//
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);
while (string && !isspace(string[i++]))
{
printf("%c",string);
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;
}
 

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,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top