int main(){char buf[100];char i =0;char tag=0;int fp;char*val[10];char*rval[10];unsignedint a =18440;unsignedint d =1;memset(buf,0,10);
tag=1;
fp = open("/dev/dd",O_RDWR);if(fp<0){printf("file failed to open\n");}sprintf(val[1],"%d%x%x",tag,a,d);
write(fp,val[1],strlen(val[1]));sprintf(rval[1],"%d%x",tag,a);// statement that causes segmentation faultreturn0;}
Now if I add one more sprintf function as shown above , it leads to segmentation fault.I do not know why?. If I remove this line, the code works fine. What could be the problem? The same problem happens when I call sprintf and write as a subfunction call?
I'm not at a compiler right now but at a glance, should there be a 'close' to match the 'open' statement?
Also (again this is from my memory only so treat as suspect!) should 'fp' be declared as a pointer?
val[1] gets assigned to the concatenated values of tag,a and d. This works fine for me. Now by adding only rval[1] statement leads to fault. Do not understand correlation. rval[1] holds tag and a in string format. Also checked the syntax and everything works without the second sprintf statement as shown in my code.
Presuming you didn't omit part of the code, please show me which string storage is addressed by pointers val[1] and rval[1].
Using uninitialized pointers isn't a syntax rather than a sematic error. Some compilers might flag a warning.