• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

segmentation fault

#include<stdio.h>
char *func()
{
      
      char *a="hari";
      char *b;
      strcpy(b,a);
      return b;
}
main()
{
      
      char *c;
      c = func();
      printf("%s",c);
      
}

this code produces segmentation fault.but if i take a[100]. then the code works fine
i am missing some concept here. please help
0
nagaharikola
Asked:
nagaharikola
1 Solution
 
ZoppoCommented:
Hi nagaharikola,

to copy a string to another you have to allocate enough memory for the destination string, in you sample that char* b - try it somehow like this:
      char *a="hari";
      char *b = (char*)malloc( strLen( a ) + 1 ); // length of 'a' + terminating 0-char
      strcpy(b,a);

Open in new window

BTW, to just duplicate a string you can even use strdup, i.e.:
char *func()
{
      char *a="hari";
      return strdup( a );
}

Open in new window

Further you have to release the allocated memory in the calling function, so add this line to your main after the printf statement:
 free( c );

Open in new window

Hope that helps,

ZOPPO
0
 
Hugh McCurdyCommented:
nagaharikola, are you trying to solve a problem (which Zoppo addressed) or are you trying to understand a concept?

~Hugh
0
 
Hugh McCurdyCommented:
(It occurs to me that the fall semester has started at many colleges.)
0
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

 
nagaharikolaAuthor Commented:
I am trying to understand the concept.
0
 
ZoppoCommented:
To learn how pointers in C work and how they are use with strings you can find a lot of info when you search the internet for something like 'tutorial c char pointer', i.e. http://www.codingunit.com/c-tutorial-strings-and-string-library-functions or http://www.cplusplus.com/doc/tutorial/pointers/
0
 
Hugh McCurdyCommented:
I find that a code example will sometimes help a student.  Please let me know if this example helped you or not.  (I'm a teacher and it helps me to know what helps and doesn't help learners.)  Feel free to ask any question you want about what the sample program is doing.

# include <stdio.h>

int main()
{
  char ch = 'x';
  char ch_array[] = "hello world!";
  char *pch_array = NULL;

  printf ( "sizeof(ch) = %d\n", sizeof(ch) );
  printf ( "sizeof(ch_array) = %d\n", sizeof(ch_array) );
  printf ( "sizeof(pch_array) = %d\n\n", sizeof(pch_array) );

  printf ( "ch = %c\n", ch );
  printf ( "ch_array = %s\n\n", ch_array );

  printf ( "address of ch = %u\n", &ch );
  printf ( "address of ch_array = %u\n", &ch_array );
  printf ( "address pointed to by pch_array = %u\n\n", pch_array );

  printf ( "Setting pch_array to point to ch_array\n" );
  pch_array = ch_array;
  printf ( "address pointed to by pch_array = %u\n\n", pch_array );

  printf ( "string pointed to by pch_array = %s\n", pch_array );
  printf ( "character pointed to by pch_array = %c\n", *pch_array );
  printf ( "character pointed to by (pch_array+6) = %c\n", *(pch_array+6) );

  return 0;
}

Open in new window


Output

sizeof(ch) = 1
sizeof(ch_array) = 13
sizeof(pch_array) = 4

ch = x
ch_array = hello world!

address of ch = 3219661435
address of ch_array = 3219661422
address pointed to by pch_array = 0

Setting pch_array to point to ch_array
address pointed to by pch_array = 3219661422

string pointed to by pch_array = hello world!
character pointed to by pch_array = h
character pointed to by (pch_array+6) = w

Open in new window

0
 
vinhnlCommented:
Hi Nagaharikola,

If you declare:

char *a="hari";
char *b;

You mean that:

a points to a memory space ['h','a','r','i',0].
b points to any address. Maybe content of this address is ['N', 'a', 'g', 'a', 'h', 'a', 'r', 'i', 'k', 'o', 'l', 'a', 0] like your name.

When you call strcpy(b,a), this function will copy from begin of content of b to a util caracter 0. In this case, free space of a is so small (5 chars). It produces a segmentation fault because the tail of b is copied into a private memory (memory of another variable or of running program).

If you declare a[100], your machine allocate for you 100 caracters. This space memory is enough for you copy from variable b to a. But if b points to a content no end (it means that a space with 1000 caracters 'x' for example), it also produces a segmentation fault like your code.

If you run your code 1000 times, I believe that you will meet sometime segmentation fault.

Hope this helps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now