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;
}
char c = (char)getchar();==>int c = getchar();. Go with the types provided, follow that through your own functions. Very few (any?) library functions either take or return thechartype. Also note that'a'is of typeint. - Weather Vanevoid main(void)which includessystem("pause");- Weather Vanegetchar()in that case. - Weather Vane