-2

Code:

int main()
{
  char *name=NULL;
  int n;
  printf("\nenter the string\n");
  scanf("%s",name);
  n=strlen(name);
  printf("%d",n);
  return 0;
}

I am getting segmentation fault. Whats wrong with the code? I have included stdio.h, stdlib.h, string.h.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Hima
  • 11,268
  • 3
  • 22
  • 31
  • *`I have included stdio.h, stdlib.h, string.h`* --You should read: [What is segmentation fault](http://en.wikipedia.org/wiki/Segmentation_fault) – Grijesh Chauhan Sep 12 '13 at 14:16

4 Answers4

8

You didn't allocate any memory for pointer to char name.

Example:

char * name = malloc( sizeof( char ) * MAX_STRING_LENGTH ) ;
this
  • 5,229
  • 1
  • 22
  • 51
  • so if i allocate say 100 chars memory and take string of size 5 .. what would happen to rest of the allocation ? – Hima Sep 12 '13 at 12:26
  • @Hima That memory would be unused but still available for later usage via the pointer. – this Sep 12 '13 at 12:27
  • If you want to make allocation perfect, then count the character contents, make new character array for that count, copy contents to this new array and deallocate previous one – Voonic Sep 12 '13 at 12:32
  • @this., You do _know_ that `sizeof (char)` is `1` by definition, and there is therefore no point in multiplying by it? – This isn't my real name Sep 12 '13 at 17:40
  • 1
    @ElchononEdelson Consistency > Laziness – this Sep 13 '13 at 00:02
2

C is not a managed language, so you need to tell your string (char *) wihch lenght of memory are you giving it. Here comes the malloc function.

By the way, there is no GarbageCollector, so you'll need to free your char * when you'll have finish to use it.

But be careful, malloc can return null, so your char * would be unable to store any char !

int main(int argc, char **argv)
{
    char *name = null;
    // Malloc your char *
    if ((name = malloc(sizeof( char ) * LENGTH_OF_YOUR_LARGER_INPUT)) == null)
    return;
    int n;
    printf("\nenter the string\n");
    scanf("%s",name);
    n=strlen(name);
    printf("%d",n);
    // Free the allocated memory to your char *
    free(name);
}
ImAGuest
  • 21
  • 1
1

Try this,

int main()

{
  char *name = malloc(sizeof( char ) * LENGTH); // define LENGTH as you desired

  int n;
  printf("\nenter the string\n");
  scanf("%s",name);
  n=strlen(name);
  printf("%d",n);

  free(name);
}

Problem is you did not allocate memory for pointer. So allocate memory tp pointer with malloc(BUFSIZE). Also at the end you have to free your allocated memory with free(name).

ANjaNA
  • 1,404
  • 2
  • 16
  • 29
1

Instead of char * name = malloc( sizeof( char ) * MAX_STRING_LENGTH ) ;

USE

 char * name = malloc(MAX_STRING_LENGTH+1 ); //+1 is to store Null character 

and sizeof(char)==1 so you can avoid it.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 2
    You're correct that `sizeof(char) == 1`, but you're wrong about `malloc()`. In C, you should [not](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) [cast](http://stackoverflow.com/questions/953112/should-i-explicitly-cast-mallocs-return-value) [malloc()](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc)'s [return](http://stackoverflow.com/questions/4993327/is-typecast-required-in-malloc) [value](http://stackoverflow.com/questions/3477741/why-does-c-require-a-cast-for-malloc-but-c-doesnt). – This isn't my real name Sep 12 '13 at 17:45
  • @ElchononEdelson helpful info. i will update answer. thank you. – Gangadhar Sep 12 '13 at 18:21