I'm new to programming and I just recently started learning C.
Every week we have "labs" where we have 90 mins to finish the given tasks and so far every week I get the same error "Exception thrown: read access violation." My question is, what does it mean? I can't seem to figure out what it actually means and how to solve it. Every week I spend half of the given time just trying to figure out what's wrong. I would really appreciate if someone explained to me what to do/what to look for when I get this error.
Once the error was gone after I fixed in my code some passing of a pointer/address (not sure which one but it was wrong). When I fixed that, the error was gone.
Another time the error was gone after I noticed that one of my loops doesn't stop where it should it just went on to very high numbers (no, not forever, I don't know why). When I fixed the loop the error was gone.
I hope it's clear what I'm trying to ask but I will include my current problem but I don't think it's really necessary to look at. Thanks in advance for any help!
This time I tried to write a task at home and once again I got the same error but I haven't yet figured out what is wrong. The task was to:
Write function:
int find_next_word(const char *string, int startIndex, int *end);
which will search string for the first word, starting at startIndex, return the index of its first character. A word is a nonempty sequence of alphanumeric characters, surrounded by other non-alphanumeric characters, beginning or end of the string (use function is_alphanumeric to classify characters). Function should also search for a first character after the word (possibly the null terminating character) and store its index in the variable pointed to by end.
is_alphanumeric is a function from a previous task which I wrote and is included in the code; it works.
#include <stdio.h>
#include <stdlib.h>
int is_alfanumeric(char c);
int find_next_word(const char* str, int start, int* end);
void main(void)
{
/********Part 1********/
puts("********Part 1********");
char c = (char)getchar();
if (is_alfanumeric(c))
printf("%c is a letter or a number\n", c);
else
printf("%c is neither a letter nor a number\n", c);
/********Part 2********/
puts("********Part 2********\n");
char str[] = " ThhHhis is MmmMy,\t tTesttt string \t \n";
printf("Original string:\n[%s]\n", str);
int end;
int start = find_next_word(str, 0, &end);
printf("First word start: %d, end: %d\n", start, end);
start = find_next_word(str, end, &end);
printf("Second word start: %d, end: %d\n", start, end);
system("pause");
}
int is_alfanumeric(char c) {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')
return 1;
else
return 0;
}
int find_next_word(const char* str, int start, int* end) {
int i = start;
for (; is_alfanumeric(str[i]); i++);
for (; !is_alfanumeric(str[i]) && str[i] != '\0'; i++);
return i;
for (; is_alfanumeric(str[i]); i++);
*end = i;
}
You're running past yourself with your loops. In your first loop, you run straight to the end of the char array. (Remmember, C doesn't have "strings", it has character arrays.) Then, in your second loop, you get an access violation because you're trying to index past the end of the array. Try this:
int find_next_word(const char *str, int start, int *end) {
int i = start;
int r = 0;
for (; str[i] != '\0'; i++) {
/* You want the FIRST character that satisfies the conditions */
if (is_alfanumeric(str[i]) {
r = i;
break;
}
}
if (r > start) {
/* If r is greater than start, you know you found a character
before the end of the array, so now you can increment
your cursor until you find a non-alfanumeric character,
or you run out of array. */
for (; !is_alfanumeric(str[i]); i++);
}
*end = i;
return r;
}