|
I am getting coredump at strncpy whats the matter and solution can anyone figure out . Its urgent please help
I am pasting code which give coredump.
2 #include <stdio.h>
3 #include <string.h>
4
5 int main ()
6 {
7 char *value = "To be or not to be";
8 char const *newValue = "Hello";
9
10 size_t newSize, valueSize;
11 newSize = newValue ? ( strlen( newValue ) + 1 ) : 1;
12
13 valueSize = 9;
14
15 if( newSize > 1 )
16 {
17
18 if( value )
19 {
20
21 free( value );
22 value = NULL;
23 }
24
25 value = (char *)malloc(newSize);
26 if( value )
27 valueSize = newSize;
28 else
29 valueSize = 0;
30 }
31
32 if( newValue == NULL )
33 {
34 if( value )
35 *value = '\0';
36 }
37 else
38 {
39
40 strncpy( value, newValue, newSize );
value[ newSize - 1 ] = '\0';
42 }
43
44
45
46 /* str2 = (char *)malloc(8);
47 strncpy(str2,str1,3);
48 str2[3]='\0';*/
49 printf("empty string : ");
50 puts (value);
51 printf(" after string : ");
52 return 0;
53 }
|