Hi All,
I tried to understand the reason for core dump but I could not get the reason. Could anyone help me to figure out the issue.
[08:49:22]/home/e025400/sathish <-> cc -o arrofptr arrofptr.c
"arrofptr.c ", line 8: warning: null dimension: ptr
[08:49:28]/home/e025400/sathish <-> arrofptr
The value of i is 0
The string is Hello
The value of i is 1
The string is Hello
out 2
Segmentation Fault(coredump)
If I give dimension at "line no 8" then the program works fine else it dumps core after printing out all the values correctly. Please let me know the reason.
And I have a basic doubt, who argv[] in main() works.
Thanks & Regards
Sathish Kumar
I tried to understand the reason for core dump but I could not get the reason. Could anyone help me to figure out the issue.
Code:
1 #include <stdio.h>
2 #include <string.h>
3 #include <malloc.h>
4
5 int main()
6 {
7 int i;
8 char *ptr[]; --> If dimension is given then the program
is working fine.
9
10 for ( i = 0; i < 2; i++ )
11 {
12 ptr[i] = malloc(6);
13
14 memcpy(ptr[i], "Hello", 6);
15 }
16
17 for ( i = 0; i < 2; i++ )
18 {
19 printf("The value of i is %d\n", i);
20 printf("The string is %s\n", ptr[i]);
21 free(ptr[i]);
22 }
23
24 printf("out %d\n", i);
25
26 return 0;
27 }
"arrofptr.c ", line 8: warning: null dimension: ptr
[08:49:28]/home/e025400/sathish <-> arrofptr
The value of i is 0
The string is Hello
The value of i is 1
The string is Hello
out 2
Segmentation Fault(coredump)
If I give dimension at "line no 8" then the program works fine else it dumps core after printing out all the values correctly. Please let me know the reason.
And I have a basic doubt, who argv[] in main() works.
Thanks & Regards
Sathish Kumar
Comment