Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate string length using this code in visual studio 2008,the code is
C++
void main(){
      char *str;
      int count = 0,i;
      
      printf("\nEnter the String: ");
      gets(str);
      for(i=0;str[i]!='\0';i++){
            count++;
      }
      printf("\nThe length of the string is %d.",count);
      getch();
}


but I am getting error
C++
"Unhandled exception at 0x6a4794af (msvcr90d.dll) : 0xC0000005: Access violation writing location 0x013f575d."

How to solve please help
[Updated]
C#
void main(){
      char *str="";
      int count = 0,i;

      printf("\nEnter the String: ");
      gets(str);
      for(i=0;str[i]!='\0';i++){
            count++;
      }
      printf("\nThe length of the string is %d.",count);
      getch();
}

but it giving same error please help
Posted
Updated 20-Mar-13 2:17am
v3

You are passing to gets an uninitialized pointer. So gets tries to write its output to some weird memory location. That's what the "Access violataion" comes from.

Allocate a buffer for the line that you want to read and pass that buffer to gets, for example:
C++
char lineBuffer[200];
gets (lineBuffer);

Just as a note for the time when you are getting more advanced in C++: There are better ways to read a line than gets. gets has the disadvantage that you can't tell it, how long your buffer is; and if your input is long enough it therefore overwrites the buffer end and corrupts your memory.
 
Share this answer
 
It's because of the way you are declaring and using str - there is no memory allocated to it in your code.
See the example here cplusplus.com reference[^] for one way of doing it but you would be better off using fgets see

this post [^] for a suggestion using that
 
Share this answer
 
It looks like you're passing an uninitialized pointer to gets.

Try replacing the line
char* str;

with
char str[ 81 ]; //room for 80 chars + '\0'

See if this gets you any further.
 
Share this answer
 
v2
Comments
debarunb 20-Mar-13 11:01am    
but I am declare it as char* str="" but same error happens
Matthew Faithfull 20-Mar-13 12:13pm    
That doesn't quite follow my suggestion. char* str="" creates a pointer to an empty string consisting of a single 0 byte. There's no room in that single byte for gets to write the characters you enter so you need to give it the array of character space (multiple bytes) it requires or it overruns into memory it's not allowed to access and that's what causes the crash.
Hi,

char* str; -> this variable is not initialized.

Try this:
C#
char *str = (char*)malloc(256);
int count = 0,i;

printf("\nEnter the String: ");
gets(str);
for(i=0;str[i]!='\0';i++){
      count++;
}
printf("\nThe length of the string is %d.",count);
free(str);
getch();


strlen function will calculate string length for you.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900